Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Aug 1st, 2012  |  syntax: None  |  size: 0.92 KB  |  views: 101  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }