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