Solution for "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."

asp.net-mvc-5 entity-framework validation

Question

I encountered this error, it did not provide any detail as to what the root cause was but I figured out what the problem is. I wanted to share it so that others that encounter it might have success in solving the problem.

I had the following class:

    public class BankUser : IdentityUser, IUserProfile
{
    #region IUserProfile Members
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    [Required]
    [Display(Name = "Email Address")]
    [EmailAddress(ErrorMessage="Invalid Email Address")]
    public string Email { get; set; }

    [Display(Name = "Time Zone")]
    public int TimeZone { get; set; }
    public Dictionary<string, string> TimeZoneOptions
    {
        get
        {
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("(GMT -10:00) Hawaii", "-10");
            d.Add("(GMT -9:00) Alaska", "-9");
            d.Add("(GMT -8:00) Pacific Time", "-8");
            d.Add("(GMT -7:00) Mountain Time", "-7");
            d.Add("(GMT -6:00) Central Time", "-6");
            d.Add("(GMT -5:00) Eastern Time", "-5");
            d.Add("Unknown", "0");
            return d;
        }
    }

    [Display(Name = "Phone")]
    [Phone(ErrorMessage = "Invalid phone number.")]
    [StringLength(10, MinimumLength = 10, ErrorMessage = "Phone number must be 10 characters long.")]
    public string Phone { get; set; }
    #endregion
}

As you can see, I extended IdentityUser with additional properties. My view only had UserName and Password as form fields when trying to create a new user UserManager.CreateAsync(). When trying to submit the form with just the UserName and Password the system would try to validate that Email was passed since it had the Required attribute.

        //
    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new BankUser() { UserName = model.UserName };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInAsync(user, isPersistent: false);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                AddErrors(result);
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }
1
3
2/2/2014 10:23:22 PM

Accepted Answer

Solution I must either add the Email as a field in my form or remove the Required attribute. Once I did either of them the code worked as expected.

Hope that helps.

1
2/2/2014 10:23:48 PM

Popular Answer

I think you're passing null value when create ApplicationUser instance. In my case, i set Postcode isn't required in ViewModel, but in IdentityModel PostCode's still required. So, you should remove Required annotation of PostCode in IdentityModel. And add Migration to Update Database.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow