Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ismaelgasparin/a85bc2f30d541c4a1ddcd2b11578b67b to your computer and use it in GitHub Desktop.
Save ismaelgasparin/a85bc2f30d541c4a1ddcd2b11578b67b to your computer and use it in GitHub Desktop.
Capturar mensagens de erro de DbEntityValidationException - Facebook - https://www.facebook.com/guilherme.stracini/posts/3007064412683796:5
public static string Parse(this DbEntityValidationException e)
{
var builder = new StringBuilder();
foreach (var eve in e.EntityValidationErrors)
{
builder.AppendFormat("Entity type: {0} | State: {1}",eve.Entry.Entity.GetType().Name,eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
builder.AppendFormat("Property name: {0} | Current value: {1} | Error message: {2}",
ve.PropertyName,
eve.Entry.CurrentValues.GetValue<Object>(ve.PropertyName),
ve.ErrorMessage);
}
return builder.ToString();
}
//Just add this method in a static class.
//Then just call it with the DbEntityValidationException inside a try/catch block to get the message.
//
//try {
// _db.SaveChanges();
//} catch(DbEntityValidationException e) {
// var message = e.Parse();
// //do what you want with message variable (log or show to the user - do not show this in a production environment!)
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment