Validation Failed For One or More Entities: MVC/Entity Framework 5.0

Today I encountered the following error during data insertion with Entity Framework as per the Title shown.

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

There could be various causes of such an issue. Here I am talking about one of them. In the database I restricted the Name column data Type to nvarchar(10) and I was inserting the value that has more than 10 characters.



As soon as I try to insert a value it generates an error as depicted below:



I have used the code as shown below to identify the root cause.

  1. public ActionResult Create(EmpRegistration collection)  
  2. {  
  3.     try  
  4.     {  
  5.          
  6.     }  
  7.     catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)  
  8.     {  
  9.         Exception raise = dbEx;  
  10.         foreach (var validationErrors in dbEx.EntityValidationErrors)  
  11.         {  
  12.             foreach (var validationError in validationErrors.ValidationErrors)  
  13.             {  
  14.                 string message = string.Format("{0}:{1}",  
  15.                     validationErrors.Entry.Entity.ToString(),  
  16.                     validationError.ErrorMessage);  
  17.                 // raise a new exception nesting  
  18.                 // the current instance as InnerException  
  19.                 raise = new InvalidOperationException(message, raise);  
  20.             }  
  21.         }  
  22.         throw raise;  
  23.     }  

This code helps you to trace the exact error. The way it suggested to me was that “The field Name must be a string or array type with a maximum length of '10'.”



This is the complete code that runs perfectly as.I wish this code will help you sometime.

  1. public ActionResult Create(EmpRegistration collection)  
  2. {  
  3.     try  
  4.     {  
  5.         if (ModelState.IsValid)  
  6.         {  
  7.             EmpRegistration empRegis = new EmpRegistration();  
  8.             // TODO: Add insert logic here  
  9.             empRegis.Address = collection.Address;  
  10.             empRegis.City = collection.City;  
  11.             empRegis.Id = 7;  
  12.             empRegis.Name = collection.Name;  
  13.             objEnity.EmpRegistrations.Add(empRegis);  
  14.             objEnity.SaveChanges();  
  15.   
  16.             return View();  
  17.         }  
  18.         return View(objEnity.EmpRegistrations);  
  19.     }  
  20.     catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)  
  21.     {  
  22.         Exception raise = dbEx;  
  23.         foreach (var validationErrors in dbEx.EntityValidationErrors)  
  24.         {  
  25.             foreach (var validationError in validationErrors.ValidationErrors)  
  26.             {  
  27.                 string message = string.Format("{0}:{1}",  
  28.                     validationErrors.Entry.Entity.ToString(),  
  29.                     validationError.ErrorMessage);  
  30.                 // raise a new exception nesting  
  31.                 // the current instance as InnerException  
  32.                 raise = new InvalidOperationException(message, raise);  
  33.             }  
  34.         }  
  35.         throw raise;  
  36.     }  

To learn more about MVC please go through the following link.

MVC Articles

Enjoy coding and reading.


Similar Articles