打电话时DbContext.SaveChanges
,我得到一个 DbUpdateException:
未处理的类型异常
“System.Data.Entity.Infrastruct.DbUpdateException”发生在
实体框架.dll。附加信息:发生错误
更新条目。有关详细信息,请参阅内部异常。
不幸的是,没有内部例外(至少,据我所知没有)。有什么方法可以确切地了解原因SaveChanges
抛出异常?至少,了解发生错误时 SaveChanges 尝试更新哪些表会很有帮助。
这是我对 SaveChanges 的覆盖。它为我提供了一个放置断点的有用位置:
public override int SaveChanges()
{
try
{
return base.SaveChanges();
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
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)
{
Debug.WriteLine(@"- Property: ""{0}"", Error: ""{1}""",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
catch(DbUpdateException e)
{
//Add your code to inspect the inner exception and/or
//e.Entries here.
//Or just use the debugger.
//Added this catch (after the comments below) to make it more obvious
//how this code might help this specific problem
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
throw;
}
}
参考:
一个或多个实体的验证失败。有关更多详细信息,请参阅“EntityValidationErrors”属性
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)