SHARE
TWEET

Untitled

a guest Aug 1st, 2012 274 Never
  1. EF Code First: How do I see 'EntityValidationErrors' property from the nuget package console?
  2. Add-Migration "remigrate" ; Update-Database;
  3.        
  4. /// <summary>
  5. /// Wrapper for SaveChanges adding the Validation Messages to the generated exception
  6. /// </summary>
  7. /// <param name="context">The context.</param>
  8. private void SaveChanges(DbContext context)
  9. {
  10.     try
  11.     {
  12.     context.SaveChanges();
  13.     }
  14.     catch (DbEntityValidationException ex)
  15.     {
  16.     StringBuilder sb = new StringBuilder();
  17.  
  18.     foreach (var failure in ex.EntityValidationErrors)
  19.     {
  20.         sb.AppendFormat("{0} failed validationn", failure.Entry.Entity.GetType());
  21.  
  22.         foreach (var error in failure.ValidationErrors)
  23.         {
  24.         sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
  25.         sb.AppendLine();
  26.         }
  27.     }
  28.  
  29.     throw new DbEntityValidationException("Entity Validation Failed - errors follow:n" + sb.ToString());
  30.     }
  31. }
RAW Paste Data
Top