FormattedDbEntityValidationException.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity.Validation;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace CD67.ModeleMVC.Entity.Internal
  8. {
  9. public class FormattedDbEntityValidationException : Exception
  10. {
  11. public FormattedDbEntityValidationException(DbEntityValidationException innerException) :
  12. base(null, innerException)
  13. {
  14. }
  15. public override string Message
  16. {
  17. get
  18. {
  19. var innerException = InnerException as DbEntityValidationException;
  20. if (innerException != null)
  21. {
  22. StringBuilder sb = new StringBuilder();
  23. sb.AppendLine();
  24. sb.AppendLine();
  25. foreach (var eve in innerException.EntityValidationErrors)
  26. {
  27. sb.AppendLine(string.Format("- Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
  28. eve.Entry.Entity.GetType().FullName, eve.Entry.State));
  29. foreach (var ve in eve.ValidationErrors)
  30. {
  31. sb.AppendLine(string.Format("-- Property: \"{0}\", Value: \"{1}\", Error: \"{2}\"",
  32. ve.PropertyName,
  33. eve.Entry.CurrentValues.GetValue<object>(ve.PropertyName),
  34. ve.ErrorMessage));
  35. }
  36. }
  37. sb.AppendLine();
  38. return sb.ToString();
  39. }
  40. return base.Message;
  41. }
  42. }
  43. }
  44. }