ASP.NET MVC EntityValidationErrors

ASP.NET MVC EntityValidationErrors


I have the error "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.".


20201127-entityvalidationerrors-1.PNG

This was working with my previous model, but I made a few changes.


The exception is thrown in the following code snippet as shown below.


20201127-entityvalidationerrors-2.PNG

try
{
    // Your Code ...
    context.SaveChanges();
}
catch (DbEntityValidationException ex)
{
    string error = "";
    foreach (var eve in ex.EntityValidationErrors)
    {
        error += string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
            eve.Entry.Entity.GetType().Name, eve.Entry.State);
        foreach (var ve in eve.ValidationErrors)
        {
            error += string.Format("- Property: \"{0}\", Error: \"{1}\"",
                ve.PropertyName, ve.ErrorMessage);
        }
    }
}


"Entity of type \"log\" in state \"Added\" has the following validation errors:- Property: \"status\", Error: \"The field status must be a string or array type with a maximum length of '1'.\""


20201127-entityvalidationerrors-3.PNG

In my model, I have a field called status. The status field only accepts a maximum of 1 character. But since I have sent a value of 3 characters to the status field, I get the EntityValidationErrors exception error.


You may have exceeded the maximum number of characters that can be entered for the string type fields you use.