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.
- public ActionResult Create(EmpRegistration collection)
- {
- try
- {
- }
- catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
- {
- Exception raise = dbEx;
- foreach (var validationErrors in dbEx.EntityValidationErrors)
- {
- foreach (var validationError in validationErrors.ValidationErrors)
- {
- string message = string.Format("{0}:{1}",
- validationErrors.Entry.Entity.ToString(),
- validationError.ErrorMessage);
- // raise a new exception nesting
- // the current instance as InnerException
- raise = new InvalidOperationException(message, raise);
- }
- }
- throw raise;
- }
- }
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.
- public ActionResult Create(EmpRegistration collection)
- {
- try
- {
- if (ModelState.IsValid)
- {
- EmpRegistration empRegis = new EmpRegistration();
- // TODO: Add insert logic here
- empRegis.Address = collection.Address;
- empRegis.City = collection.City;
- empRegis.Id = 7;
- empRegis.Name = collection.Name;
- objEnity.EmpRegistrations.Add(empRegis);
- objEnity.SaveChanges();
- return View();
- }
- return View(objEnity.EmpRegistrations);
- }
- catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
- {
- Exception raise = dbEx;
- foreach (var validationErrors in dbEx.EntityValidationErrors)
- {
- foreach (var validationError in validationErrors.ValidationErrors)
- {
- string message = string.Format("{0}:{1}",
- validationErrors.Entry.Entity.ToString(),
- validationError.ErrorMessage);
- // raise a new exception nesting
- // the current instance as InnerException
- raise = new InvalidOperationException(message, raise);
- }
- }
- throw raise;
- }
- }
To learn more about MVC please go through the following link.
MVC Articles
Enjoy coding and reading.

Abdul Amin KhanPosted Jun 26, 2020, 2:21 AM
Thanks very nice article. very Helpful to get exact error from Entity
rentacar formePosted Apr 4, 2020, 2:44 PM
Thanks for this it helped me alot
Pergin SheniPosted May 21, 2019, 3:48 AM
Thanks for this. Really it very useful to find which column having issue when db change.
Prasad KulkarniPosted Aug 27, 2018, 6:17 AM
I am getting this Exception please Help me,Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Prasad KulkarniPosted Aug 27, 2018, 6:16 AM
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
karthiPosted Dec 4, 2017, 7:57 PM
Thank you very much. saved a lot of time..easy to identify the model errors
Annath MPosted Jul 7, 2017, 1:14 AM
Thanks for your sharing. It makes me to identify which entity cause the error
Keno MarioPosted Jun 20, 2017, 12:00 PM
Thank you! Works like a charm and saves a lot of time.
Manav PandyaPosted Dec 16, 2016, 10:10 AM
In db.SaveChanges() , reply me soon so i can give you code
Manav PandyaPosted Dec 16, 2016, 10:09 AM
Where i got error :: if (ModelState.IsValid) { db.Students.Add(student); db.SaveChanges(); return RedirectToAction("Index"); }
Manav PandyaPosted Dec 16, 2016, 10:09 AM
I got same error but with different reason thats what i dont know Sachin Kalia
Kiran NaveenaPosted Jun 8, 2016, 6:22 AM
Thanks a lot Bro. U saved my time.
Thiruppathi RPosted May 31, 2016, 9:00 AM
Nice info ...
hari prasad CSPosted May 20, 2016, 2:59 AM
Thank you :)
PANKAJ KUMARPosted Oct 20, 2015, 1:06 AM
Thanks Dear, It saved my time..
Khargesh RajputPosted Aug 26, 2015, 7:07 AM
thanks it helps me
khawaja AtteeqPosted Oct 28, 2014, 3:17 PM
Thanks mate, I was pissed , I wasted my four hours on this issue