Created
April 22, 2020 20:45
-
-
Save guibranco/3acf5873c5b0f352cc63c6aa31993b7c 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
This file contains hidden or 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
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