【问题标题】:Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Code First一个或多个实体的验证失败。有关更多详细信息,请参阅“EntityValidationErrors”属性。代码优先
【发布时间】:2013-05-13 04:05:11
【问题描述】:

我收到此错误:

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

当我尝试在包管理器控制台中使用Update-Database 命令更新数据库时。

如何将行写入 Visual Studio 的输出窗口?

我试过了:

try
{
    context.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
    foreach (var eve in e.EntityValidationErrors)
    {
        System.Diagnostics.Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
            eve.Entry.Entity.GetType().Name, eve.Entry.State);
        foreach (var ve in eve.ValidationErrors)
        {
            System.Diagnostics.Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                ve.PropertyName, ve.ErrorMessage);
        }
    }
    throw;
}

但这没有用。有关如何调试此问题的任何其他建议?

【问题讨论】:

    标签: ef-code-first entity-framework-5 nuget-package


    【解决方案1】:

    我不知道为什么写入 VS 输出窗口不起作用以及如何使它起作用。但作为最后的手段,只需将错误写入一个文本文件,该文件应该独立于您拥有的应用程序类型:

    try
    {
        context.SaveChanges();
    }
    catch (System.Data.Entity.Validation.DbEntityValidationException e)
    {
        var outputLines = new List<string>();
        foreach (var eve in e.EntityValidationErrors)
        {
            outputLines.Add(string.Format(
                "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
            foreach (var ve in eve.ValidationErrors)
            {
                outputLines.Add(string.Format(
                    "- Property: \"{0}\", Error: \"{1}\"",
                    ve.PropertyName, ve.ErrorMessage));
            }
        }
        //Write to file
        System.IO.File.AppendAllLines(@"c:\temp\errors.txt", outputLines);
        throw;
    
        // Showing it on screen
        throw new Exception( string.Join(",", outputLines.ToArray()));
    
    }
    

    【讨论】:

    • @Slauma:谢谢.. 好技巧.. 从来没想过.. 这就是优秀开发人员和非常优秀开发人员之间的区别所在! ;)
    【解决方案2】:

    您可以像下面这样将它向上传递到异常堆栈。

    try
    {
        _dbContext.SaveChanges();
    }
    catch (DbEntityValidationException dbValEx)
    {
       var outputLines = new StringBuilder();
       foreach (var eve in dbValEx.EntityValidationErrors)
       {
         outputLines.AppendFormat("{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:"
           ,DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State);
    
         foreach (var ve in eve.ValidationErrors)
         {
           outputLines.AppendFormat("- Property: \"{0}\", Error: \"{1}\""
            ,ve.PropertyName, ve.ErrorMessage);
         }
       }
    
     throw new DbEntityValidationException(string.Format("Validation errors\r\n{0}"
      ,outputLines.ToString()), dbValEx);
    }
    

    【讨论】:

    • 谢谢,我会在下一个项目中尝试一下! +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 2011-12-09
    • 1970-01-01
    • 2021-05-25
    • 2018-01-10
    • 2014-02-24
    • 1970-01-01
    相关资源
    最近更新 更多