Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check EntityValidationErrors when validation fails?

I get this message when I try to edit a property in MVC 4 database first project. I'm using the MVC default edit page.

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

Where do I check for validation?

like image 436
user2470369 Avatar asked Jun 10 '13 09:06

user2470369


People also ask

How do you solve this error validation failed for one or more entities see EntityValidationErrors property for more details?

Error: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. solution: correct your Data Model according to your database SET fieldNames or length.

What is validation failed for one or more entities?

EntityValidationErrors is a collection which represents the entities which couldn't be validated successfully, and the inner collection ValidationErrors per entity is a list of errors on property level. These validation messages are usually helpful enough to find the source of the problem. While debugging Debug.


2 Answers

Go to your edit function, put a try - catch block and catch the exception - 'DbEntityValidationException'

if you want to see the errors, iterate though the validation errors.

here is a simple code example.

catch (DbEntityValidationException ex)
{
    foreach (var errors in ex.EntityValidationErrors)
    {
        foreach (var validationError in errors.ValidationErrors)
        {
             // get the error message 
            string errorMessage = validationError.ErrorMessage;
        }
    }
}
like image 50
wizzardz Avatar answered Oct 24 '22 21:10

wizzardz


If you set a break point in your controller you can check which values have errors against them by looking in the ModelState. The ModelState.Values collection contains the error and the key is the field.

like image 22
hutchonoid Avatar answered Oct 24 '22 22:10

hutchonoid