我想得到所有验证错误。
我可以在 DbEntityValidationException 中得到错误吗?
DbEntityValidationException ex = exception as DbEntityValidationException
我想得到所有验证错误。
我可以在 DbEntityValidationException 中得到错误吗?
DbEntityValidationException ex = exception as DbEntityValidationException
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();
}