Wednesday, November 8, 2017

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

This has a good fix for this error. In the partial for modifications which has a sister partial class that inherits from DbContext put something like so:

public override int SaveChanges()
{
   try
   {
      return base.SaveChanges();
   }
   catch (DbEntityValidationException ex)
   {
      var x = ex.EntityValidationErrors.SelectMany(y => y.ValidationErrors).Select(z =>
            z.ErrorMessage);
      string message = string.Join(" ", x);
      message = string.Concat(ex.Message, " The validation errors are: ", message);
      throw new DbEntityValidationException(message, ex.EntityValidationErrors);
   }
}

 
 

This will give you much better reporting. In my particular problem I had made a column/getsetter for a varchar/string not nullable when it should have been nullable. I dropped and recreated the database tables and I opened the .edmx as a big blob of XML and found the two places inside where the column was referenced and I set the nullability state from false to true.

No comments:

Post a Comment