Introduction

While running on C# app using Entity Framework, me came me the issue Validation failed for one or more entities. Today, I've prepared a whole solution to conquer issue Validation failed for one or more entities while playing with Entity Framework (MS SQL Server database) using C#, MVC, ASP.Net, VN.net etc. Recently, I've absolutely explained entity framework error Store update insert or delete statement affected an unexpected number of rows (0)  in my recent article, this day going to resolve another one entity framework error Validation failed for one or more entities.

Main Error Causes

This error primarily once there's issue with our database entities, example like we've given text message length 40 characters and attempting to insert  more than 40 characters then we'll face relevant error message whereas playing with entity framework. Here, I've actually developed how to diagnose this issue and then after get red of same issue.

Exception Event

try
{
    // doing here my logic
    _context.savechanges();
}
catch (DbEntityValidationException dbEx)
 {
    foreach (var validationErrors in dbEx.EntityValidationErrors)
      {
         foreach (var validationError in validationErrors.ValidationErrors)
           {
              Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
           }
      }
}

Error Description

DbEntityValidationException was caught
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Solution

This error happens as a result of data type length was short and I was attempting long data length to insert in to that particular column, after I went into deeply analysis then came up that data length is short in database therefore it needs to extend data storage capability. Whereas increase the length in database and save the database table then attempt once again with same input data then it starts to operate functioning fine. I've increased FirstName data Type length nvarchar(20) to nvarchar(50).

Validation failed for one or more entities
Validation failed for one or more entities

Conclusion

Error Validation failed for one or more entities often happened whereas working in MVC, C#, ASP.Net application using Entity Framework (MS SQL Database, MySQL, Oracle, MongoDB etc). Above mentioned solution in detail would help to resolve your this issue completely, if anymore help required anyone can approach me.

Relevant Reading