0
votes

I have been battling this issue for over a week now i have read articles online including StackOverflow, I came across some similar articles but none of the articles i came across solves my problem.

Below is the error:

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Description: An unhandled 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.

Here is my account controller:

// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new User() { UserName = model.Email, Email = model.Email  };
        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);
}

Below is my new code after your advice:

// POST: /Account/Register

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    try
    {
        if (ModelState.IsValid)
        {
            var user = new User() { UserName = model.Email, Email = model.Email };
            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);
    }
    catch (System.Data.Entity.Validation.DbEntityValidationException ex)
    {
        foreach (var errors in ex.EntityValidationErrors)
        {
            foreach (var validationError in errors.ValidationErrors)
            {
                // get the error message 
                string errorMessage = validationError.ErrorMessage;

                //Or log your error message here
            }
        }
        throw;
    }
}

I get the same error.

1
did u try to catch exception of type => DbEntityValidationException er-sho

1 Answers

0
votes

Try to catch exception of type DbEntityValidationException by using try/catch block to your api method,

catch (DbEntityValidationException ex)
{
    foreach (var errors in ex.EntityValidationErrors)
    {
        foreach (var validationError in errors.ValidationErrors)
        {
             // get the error message 
            string errorMessage = validationError.ErrorMessage;

            //Or log your error message here
        }
    }
}

So finally your method look like,

// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    try
    {
        if (ModelState.IsValid)
        {
            var user = new User() { UserName = model.Email, Email = model.Email  };
            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);
    }
    catch (DbEntityValidationException ex)
    {
        foreach (var errors in ex.EntityValidationErrors)
        {
            foreach (var validationError in errors.ValidationErrors)
            {
                // get the error message 
                string errorMessage = validationError.ErrorMessage;

                //Or log your error message here
            }
        }
        throw;
    }        
}

Note: Make sure at your end, above method will not give you unreachable code detected and return appropriate message or view or data.