Monday, August 11, 2014

Validation failed for one or more entities. See EntityValidationErrors property for more details.
Error-Messages
Today I encountered an error during data insertion through Entity Framework as per the Title.
There could be a various cause of such issue .Here I am talking about one of them.In database I restricted Name column data Type to nvarchar(10).
 and I was inserting value which has more than 10 character.
 
image
 
As soon as I try to insert value it generates me an error as depicted below:
 
clip_image002
.
 
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:             }
 24:         }

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



image



This is the complete code which runs perfectly as shown below .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:             }
 37:         }

To learn more about MVC please go through the following link.
MVC Articles
Enjoy coding and readingSmile

2 comments :

  1. man, THANK YOU, that was the answer to my problem, thanks to u, I could finish my project.

    ReplyDelete
  2. Thank u helped me because my size we less, i was giving more size.

    ReplyDelete