I use ASP.NET Identity 2
in an MVC 5
project and I want to update Student
data by using UserManager.Update()
method. However,
There is no need to pass the student
as ApplicationUser
to the UserManager.Update()
method (because Student
class inherits (hence is) ApplicationUser
).
The problem with your code is that you are using new Student
operator, thus creating a new student rather than updating the existing one.
Change the code like this:
// Get the existing student from the db
var user = (Student)UserManager.FindById(model.Id);
// Update it with the values from the view model
user.Name = model.Name;
user.Surname = model.Surname;
user.UserName = model.UserName;
user.Email = model.Email;
user.PhoneNumber = model.PhoneNumber;
user.Number = model.Number; //custom property
user.PasswordHash = checkUser.PasswordHash;
// Apply the changes if any to the db
UserManager.Update(user);