0
votes

I get an error every time to try to create a new user or register. Here is the source: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Description: An unhand-led exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Source Error:

Line 185: Line 186: Line 187: var result = await UserManager.CreateAsync(user, model.Password); Line 188: if (result.Succeeded) Line 189: {

Source File: C:\Users\koand\documents\visual studio 2015\Projects\MvcStore\MvcStore\Controllers\AccountController.cs Line: 187

My code:

 public async Task<ActionResult> Register(RegisterViewModel model, string returnUrl)
        {


            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName = model.Email,
                    Email = model.Email
                 };

                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
3
I suspect user or model.Password is null or user already exists or password doesn't meet length requirements or some other data issue - perhaps the Asp.Net identity tables are not accessible. Seems like the answer is in the details of the thrown exception. Can you set up your visual studio to break when a managed exception is thrown and look at the exception details - hit Ctrl-D theb E then check "Common Language Runtime Exceptions" - sevzas
"See 'EntityValidationErrors' property for more details" - Gert Arnold
Thanks, i got this error when i tried to : twoContextReset and since then i can't create a new user but i can login with the one manually into the start_up/ IdentityConfig.......i ran update_database and was ok. - DERK

3 Answers

0
votes

Below are the reason of this type of exception

  1. Your model is different than DB schema

  2. DataType or constraints are not proper

  3. set of Identity column is also lead to this type of error

Make sure your run migration if you work with code-first and update EDMX if you work on DB-first approach.

0
votes

This usually happens when data values being saved to database violates field validation for example, the length of values exceeds the allowed length for the field like saving "longtext" to field that can only accept 3 characters.

0
votes

You can use this line in your dbcontext constructor for ignoring entityvalidation .

base.Configuration.ValidateOnSaveEnabled = false;