Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save OskarKlintrot/8a8d7687c98a146e4f26c7b9eeb6b248 to your computer and use it in GitHub Desktop.
Save OskarKlintrot/8a8d7687c98a146e4f26c7b9eeb6b248 to your computer and use it in GitHub Desktop.
C# Helper Methods
private static void SaveChanges(DbContext context)
{
try
{
context.SaveChanges();
}
catch (DbEntityValidationException exception)
{
var sb = new StringBuilder();
foreach (var failure in exception.EntityValidationErrors)
{
sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
foreach (var error in failure.ValidationErrors)
{
sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
sb.AppendLine();
}
}
throw new DbEntityValidationException(
"Entity Validation Failed - errors follow:\n" +
sb, exception
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment