23

Having a problem with saving user changes to the database, like changing the person's name. I'm using the IdentityModel that is automatically created in a new VS2013 web project using individual authentication. Sadly, the template doesn't allow you to change any user information, other than changing roles. I'm looking around via google, I haven't found much. Anyone implement updating using the base identity code?

This is the closest thing I found:

Updating user data - Asp.net Identity

I haven't been successful at incorporating default template. I've just started using Identity this week, so it might be my lack of understanding that's the problem.

var updatedUser = new ApplicationUser
            {
                Id = model.UserId,
                UserName = model.UserName,
                CustomerId = model.CustomerId,
                Email = model.EmailAddress,
                FirstName = model.FirstName,
                LastName = model.LastName,
                PhoneNumber = model.PhoneNumber,                    
            };

...
var result = await UserManager.UpdateAsync(updatedUser);

My UserManager is created like this:

return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();

I get the following error in the browser:

Attaching an entity of type 'ApplicationUser' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate

Thanks

Community
  • 1
  • 1
BCarlson
  • 1,122
  • 2
  • 11
  • 26
  • It might be helpful if you mentioned exactly HOW you were having problems... What did you try? What didn't work? What was the error? What was the result? Be as specific as you can. – Erik Funkenbusch Apr 03 '14 at 13:58
  • In my action, I'm creating an ApplicationUser, with the ID and other fields filled out. Then I call: – BCarlson Apr 03 '14 at 14:01
  • You might have a look at my answer on [ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value](http://stackoverflow.com/questions/23201907/asp-net-mvc-attaching-an-entity-of-type-modelname-failed-because-another-ent/39557606#39557606). – Murat Yıldız Sep 18 '16 at 12:37

2 Answers2

47

The problem I had was I creating an ApplicationUser and saved the object to the database. Since Identity uses Entity Framework under the covers, the entity state of the "updatedUser" object is Added. So Entity Framework tried to INSERT in to the Identity database, causing the conflict. So, you have to get the user and update the returned user object for Entity Framework to know that the entity state is Modified. Here's the working code:

var user = await UserManager.FindByIdAsync(model.UserId);

user.Email = model.EmailAddress;
user.CustomerId = model.CustomerId;
user.FirstName = model.FirstName;
user.PhoneNumber = model.PhoneNumber;
user.LastName = model.LastName;

var result = await UserManager.UpdateAsync(user);
BCarlson
  • 1,122
  • 2
  • 11
  • 26
  • 3
    After almost 3 days of trying to figure out why my user would not update, I stumbled across many answers and approaches. Yours my friend makes total sense and has saved me further grief... for this my friend you have won the internet! – devfunkd Jun 03 '14 at 19:43
  • 2
    hi, I did the same and still getting the same error. Any Idea ? – user123456 Sep 07 '14 at 23:18
  • Ok, to confirm. You get the user and verified that the you can see the information. Then you changed some field, like FirstName. And then finally you saved it. Is that a correct summary? Also which field did you update? – BCarlson Sep 08 '14 at 14:43
  • I am doing the same, the only difference is that I am first getting the user and then passing it to another function where I am changing details and trying to update .. and major problem is that this error is not occurring every time ... it is coming rearly. :( – NMathur May 30 '17 at 05:55
  • I've tried implementing this solution to update a user and I get an "Incorrect number of parameters" error on the UserManager object. I don't understand how this works in this case unless there was a major change since this solution was posted. – Alex May 04 '18 at 20:28
0

You can also use the AuthContext and update the state to EntityState.Modified. Below is an example. This will allow you to only make one call to the DB instead of two.

AuthContext authContext = new AuthContext(); authContext.Entry(updatedUser).State = EntityState.Modified;

divide_byzero
  • 790
  • 2
  • 9
  • 24