一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。代码第一 [英] Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Code First

查看:1011
本文介绍了一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。代码第一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

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

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

when i try to update the database with the Update-Database command in the Package Manager Console.

如何将线条写入视觉工作室的输出窗口?

How can I write the lines to the output window in 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;
}

但是没有工作。关于如何调试这些的其他建议?

But that didn't work. Any other suggestions on how to debug this?

推荐答案

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

I don't know why writing to the VS output window doesn't work and how to make it work. But as a last resort just write the errors into a text file which should work independent of the type of application you have:

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()));

}

这篇关于一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。代码第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆