我得到这个错误:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
当我尝试更新与数据库Update-Database
在Package Manager控制台命令。
我怎么能写行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()));
}
你可以通过它的异常堆栈像下面。
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);
}
文章来源: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Code First