gpt4 book ai didi

ef-code-first - 一个或多个实体的验证失败。有关更多详细信息,请参见 'EntityValidationErrors'属性。代码优先

转载 作者:行者123 更新时间:2023-12-04 04:53:27 24 4
gpt4 key购买 nike

我收到此错误:

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

当我尝试在Package Manager控制台中使用 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;
}

但这是行不通的。还有其他关于如何调试的建议吗?

最佳答案

我不知道为什么无法写入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()));

}

关于ef-code-first - 一个或多个实体的验证失败。有关更多详细信息,请参见 'EntityValidationErrors'属性。代码优先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16618334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com