Reputation: 41
This is my first time using code first in MVC5 for user management. I can change passwords but I can not update the user registration. I have combed the internet and literally look at every question on this site related to UserManager.Update(user) not working.
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
namespace _100..Models
{
public class ApplicationUser : IdentityUser
{
public virtual PersonalInfo PersonalInfo { get; set; }
public virtual BillingInfo BillingInfo { get; set; }
public virtual DeliveryInfo DeliveryInfo { get; set; }
public Chapters Chapter { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class PersonalInfo
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class BillingInfo
{
public int ID { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
public class DeliveryInfo
{
public int ID { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false){}
public DbSet<PersonalInfo> PersonalInfo { get; set; }
public DbSet<BillingInfo> BillingInfo { get; set; }
public DbSet<DeliveryInfo> DeliveryInfo { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
And here's my action
[HttpPost]
public async Task<ActionResult> UpdateRegisteration(ApplicationUser user)
{
var result = await UserManager.UpdateAsync(user);
return RedirectToAction("Index", "Home");
}
I have confirmed that the user object has the updated data when it hits the action and the update method returns success but it doesn't actually update the database.
Upvotes: 3
Views: 7301
Reputation: 41
There are several layers to this fix. First when I executed the UserManager.Update it was against the user object I received from my updateRegistration view. EF thought it was a new object and was raising an internal error "UserName already exists" and was failing but reporting success. So I had to create a dbContext and update the entity state to modified. But I learned that I also had to create a user object from the updated users ID to set the UserName or the update failed. The I found that I had to update the PasswordHash or it would be null. Then I found that I also had to update the SecurityStamp or the login process would raise an error. My Action is below.
[HttpPost]
public async Task<ActionResult> UpdateRegisteration(ApplicationUser UpdatedUser)
{
var SavedUser = await UserManager.FindByIdAsync(UpdatedUser.Id);
try
{
UpdatedUser.SecurityStamp = SavedUser.SecurityStamp;
UpdatedUser.PasswordHash = SavedUser.PasswordHash;
UpdatedUser.UserName = SavedUser.UserName;
UpdatedUser.Id = SavedUser.Id;
UpdatedUser.PersonalInfo.ID = SavedUser.PersonalInfo.ID;
UpdatedUser.BillingInfo.ID = SavedUser.BillingInfo.ID;
UpdatedUser.DeliveryInfo.ID = SavedUser.DeliveryInfo.ID;
ApplicationDbContext db = new ApplicationDbContext();
db.Entry(UpdatedUser).State = EntityState.Modified;
db.Entry(UpdatedUser.PersonalInfo).State = EntityState.Modified;
db.Entry(UpdatedUser.BillingInfo).State = EntityState.Modified;
db.Entry(UpdatedUser.DeliveryInfo).State = EntityState.Modified;
await db.SaveChangesAsync();
// var result = await UserManager.UpdateAsync(SavedUser);
return RedirectToAction("Index", "Home");
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
{
Exception raise = dbEx;
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
string message = string.Format("{0}:{1}",
validationErrors.Entry.Entity.ToString(),
validationError.ErrorMessage);
// raise a new exception nesting
// the current instance as InnerException
raise = new InvalidOperationException(message, raise);
}
}
throw raise;
}
}
Upvotes: 1