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).