3

When you add additional table for additional user information such as first name and birthdate, how do you update or edit those info in a controller? Here's my customized Identity User model:

public class ApplicationUser : IdentityUser
{
   public virtual UserEntity UserEntity { get; set;}

   public class UserInfo
   {
     // additional user properties in another table
     public int Id { get; set; }
     public string FirstName { get; set; }
     public DateTime BirthDate {get; set; }
     public virtual ApplicationUser User { get; set; }
   }
}

This model indicates that a UserInfo belongs to only one User. In short the relationship is 1:0/1 (One is to zero or 1)

To update or add more information to the user, I suppose it should be somewhat like this in the controller but it's not:

public class AccountManager : Controller
{
   [HttpPost]
   public ActionResult UpdateUserInfo(string first_name, DateTime birth_date)
   {
      string currentUserId = User.Identity.GetUserId();
      var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
      var currentUser = userManager.FindById(currentUserId);

      currentUser.UserInfo.FirstName = first_name;
      currentUser.UserInfo.BirthDate = birth_date;

      currentUser.SaveChanges();

      // some codes removed for clarity
   }
}

I hope it's clear. How do you update related model of Identity User? Also note that the User still has no existing UserInfo record (relationship is still 1:0).

doncadavona
  • 7,162
  • 9
  • 41
  • 54

1 Answers1

3

Savechanges updates when you get data from db

if you want to update you should use

currentUser.Entry(first_name).State = System.Data.EntityState.Modified;

or you can use

currentUser.Entry(existing first_name).CurrentValues.SetValues(first_name);
Khan Engineer
  • 408
  • 5
  • 23
  • The only problem i see here is that `currentUser` is not the context. I think it should maybe be `userManager.Entry(currentUser).State = EntityState.Modified;` Although i am not sure. I have not seen the context being declared like it is in the OP before. –  Apr 07 '15 at 07:25
  • Hey @Khan .. not original poster ... just a casual upvoter ;) –  Apr 07 '15 at 07:28
  • Im not the original poster for this question. I upvoted your answer because it looks correct. Shot for the link though. That 1 liner context is a bit confusing. –  Apr 07 '15 at 07:46
  • Thanks, I tried this but to me it brings this error: Attaching an entity of type 'my_project.Models.ApplicationUser' failed because another entity of the same type already has the same primary key value. This can happen when using ... – doncadavona Apr 07 '15 at 07:53
  • Thank you, I'm trying to use your answer, it looks right to me too, but an exception occurs so I'm trying to fix it. I'd update as soon as I can get it right. Thanks to @KhanEngineer – doncadavona Apr 07 '15 at 07:59
  • 1
    i would recomment you to read this ans it may help http://stackoverflow.com/questions/20444022/updating-user-data-asp-net-identity – Khan Engineer Apr 07 '15 at 08:04
  • you can also use currentUser.UpdateAsync(first_name); or userManager.UpdateAsync(first_name); – Khan Engineer Apr 07 '15 at 09:14
  • look at your tables there is one more table which has the same primary used in that table – Khan Engineer Apr 07 '15 at 09:35