FrankO
FrankO

Reputation: 2562

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

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);
    }

Upvotes: 3

Views: 14811

Answers (3)

Murat Yıldız
Murat Yıldız

Reputation: 12022

If you use Entity Framework:

Error Message (from the Update-Database command in the PMC): Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Solution: One cause of this problem is validation errors when the Seed method runs. See Seeding and Debugging Entity Framework (EF) DBs for tips on debugging the Seed method.

For more information please visit: Advanced Entity Framework 6 Scenarios for an MVC 5 Web Application (12 of 12)

Upvotes: 0

Long Nguyen
Long Nguyen

Reputation: 10936

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.

Upvotes: 0

FrankO
FrankO

Reputation: 2562

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.

Upvotes: 1

Related Questions