Sometimes in entity framework code first approach, you could have come across an issue "validation failed for one or more entities. see 'entityvalidationerrors' property for more details" while you commit your changes db using SaveChanges() method.
In such cases, you can make use of the below C# code snippet to get through the root cause of the error.
In such cases, you can make use of the below C# code snippet to get through the root cause of the error.
try
{
context.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
System.Diagnostics.Debug.WriteLine("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)
{
System.Diagnostics.Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}