19

打印日志时出现以下错误

Message: Validation failed for one or more entities. See 'EntityValidationErrors'  
property for more details.; Stack Trace:    at 
System.Data.Entity.Internal.InternalContext.SaveChanges()

EntityValidationErrors 对象包含不同节点中的完整详细错误。打印它的最佳方法是什么?

4

3 回答 3

29

更简洁的语法:

catch (System.Data.Entity.Validation.DbEntityValidationException ex)
{
     Logger.WriteError("{0}{1}Validation errors:{1}{2}", ex, Environment.NewLine, ex.EntityValidationErrors.Select(e => string.Join(Environment.NewLine, e.ValidationErrors.Select(v => string.Format("{0} - {1}", v.PropertyName, v.ErrorMessage)))));
     throw;
}
于 2013-09-11T07:59:07.787 回答
26

以下是一些扩展方法:

/// <summary>
///     A DbEntityValidationException extension method that formates validation errors to string.
/// </summary>
public static string DbEntityValidationExceptionToString(this DbEntityValidationException e)
{
    var validationErrors = e.DbEntityValidationResultToString();
    var exceptionMessage = string.Format("{0}{1}Validation errors:{1}{2}", e, Environment.NewLine, validationErrors);
    return exceptionMessage;
}

/// <summary>
///     A DbEntityValidationException extension method that aggregate database entity validation results to string.
/// </summary>
public static string DbEntityValidationResultToString(this DbEntityValidationException e)
{
    return e.EntityValidationErrors
            .Select(dbEntityValidationResult => dbEntityValidationResult.DbValidationErrorsToString(dbEntityValidationResult.ValidationErrors))
            .Aggregate(string.Empty, (current, next) => string.Format("{0}{1}{2}", current, Environment.NewLine, next));
}

/// <summary>
///     A DbEntityValidationResult extension method that to strings database validation errors.
/// </summary>
public static string DbValidationErrorsToString(this DbEntityValidationResult dbEntityValidationResult, IEnumerable<DbValidationError> dbValidationErrors)
{
    var entityName = string.Format("[{0}]", dbEntityValidationResult.Entry.Entity.GetType().Name);
    const string indentation = "\t - ";
    var aggregatedValidationErrorMessages = dbValidationErrors.Select(error => string.Format("[{0} - {1}]", error.PropertyName, error.ErrorMessage))
                                           .Aggregate(string.Empty, (current, validationErrorMessage) => current + (Environment.NewLine + indentation + validationErrorMessage));
    return string.Format("{0}{1}", entityName, aggregatedValidationErrorMessages);
}

用例 1:

catch(DbEntityValidationException e) {
    Logger.WriteError(e.DbEntityValidationExceptionToString());
    throw;      
}

应该输出:

StackTrace
Validation errores:
[EntityName]
    - [Property - ErrorMessage]
    - [Property - ErrorMessage]
[EntityName2]
    - [Property - ErrorMessage]
    - [Property - ErrorMessage]

用例 2:

catch(DbEntityValidationException e) {
    Logger.WriteError(e.DbEntityValidationResultToString());
    throw;      
}

应该输出:

[EntityName]
    - [Property - ErrorMessage]
    - [Property - ErrorMessage]
[EntityName2]
    - [Property - ErrorMessage]
    - [Property - ErrorMessage]
于 2014-01-24T09:39:59.517 回答
7

你可以试试这样的

   foreach (var failure in ex.EntityValidationErrors)
    {
       string validationErrors="";

        foreach (var error in failure.ValidationErrors)
        {
           validationErrors+=error.PropertyName+"  "+error.ErrorMessage;
        }
    }
于 2012-12-11T17:40:48.220 回答