Updating user data – ASP.NET Identity

OK… I spent hours trying to figure why userManager.updateAsync would not persist the user data that we edit … until I reached the following conclusion:

The confusion arises from the fact that we create the UserManager in one line like this:

var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new MyDbContext()));

…then we use manager.UpdateAsync( user ); but that will update the user in the context, and then we will need to save changes to the dbcontext of the Identity. So, the question is how to get the Identity DBcontext in the easiest way.

To solve this, we should not create the UserManager in one line … and here is how I do it:

var store = new UserStore<ApplicationUser>(new MyDbContext());
var manager = new UserManager(store);

then after updating the user by calling

manager.UpdateAsync(user);

then you go to the context

var ctx = store.context;

then

ctx.saveChanges();

wahooooooo…persisted 🙂

Hope this will help someone who pulled their hair for a few hours 😛

Leave a Comment