Created
April 5, 2017 09:13
-
-
Save OskarKlintrot/8a8d7687c98a146e4f26c7b9eeb6b248 to your computer and use it in GitHub Desktop.
C# Helper Methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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