I have tried to update user data in database, I am taking value from view and I assign these data to current user.Yet, I am taking an error in that line when I update user's data var result = await UserManager.UpdateAsync(userAccount);
.It is written Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. .How can I fix that? Thanks
CONTROLLER:
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ModifyAccount(ModifyUserAccountViewModel model)
{
var user = await UserManager.FindByIdAsync(int.Parse(User.Identity.GetUserId()));
UserAccount userAccount = (UserAccount)user;
userAccount.surname = model.surname;
userAccount.name = model.name;
userAccount.Email = model.email;
var result = await UserManager.UpdateAsync(userAccount);
if (result.Succeeded)
{
return RedirectToAction("Index", new { Message = "Account modyfyied successfully!" });
}
AddErrors(result);
return View("Error");
}
Startup.cs:
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
}
You can try, as suggested by @Praveen Prasad in this post, to catch the exception to see what validation errors you have:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ModifyAccount(ModifyUserAccountViewModel model)
{
var user = await UserManager.FindByIdAsync(int.Parse(User.Identity.GetUserId()));
UserAccount userAccount = (UserAccount)user;
userAccount.surname = model.surname;
userAccount.name = model.name;
userAccount.Email = model.email;
try {
var result = await UserManager.UpdateAsync(userAccount);
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}",
validationError.PropertyName,
validationError.ErrorMessage);
}
}
}
if (result.Succeeded)
{
return RedirectToAction("Index", new { Message = "Account modyfyied successfully!" });
}
AddErrors(result);
return View("Error");
}