1

I want to get all validation errors.

Can I get errors in DbEntityValidationException ?

DbEntityValidationException ex = exception as DbEntityValidationException
srdrylmz
  • 51
  • 6
  • possible duplicate of [Validation failed for one or more entities. See 'EntityValidationErrors' property for more details](http://stackoverflow.com/questions/7795300/validation-failed-for-one-or-more-entities-see-entityvalidationerrors-propert) – Colin May 27 '15 at 07:59
  • See also: http://stackoverflow.com/a/22500988/150342 – Colin May 27 '15 at 08:01

1 Answers1

1
public static string GetAllExceptionTree(Exception exception)
    {
        if (exception is DbEntityValidationException)
        {
            StringBuilder validationErrors = new StringBuilder();
            DbEntityValidationException ex = exception as DbEntityValidationException;
            foreach (var error in ex.EntityValidationErrors)
                foreach (var err in error.ValidationErrors)
                    validationErrors.Append(String.Format("{0}<br/>", err.ErrorMessage));

            return validationErrors.ToString();
        }

        StringBuilder sb = new StringBuilder();
        do
        {
            if (sb.Length != 0)
                sb.Append("...");
            sb.Append(exception.Message);
            exception = exception.InnerException;
        } while (exception != null);

        return sb.ToString();
    }
srdrylmz
  • 51
  • 6