findDBUpdateException的原因

当调用DbContext.SaveChanges ,我得到一个DbUpdateException:

“EntityFramework.dll中出现”System.Data.Entity.Infrastructure.DbUpdateException“types的未处理exception。附加信息:更新条目时发生错误,请参阅内部exception以获取详细信息。

不幸的是,没有内在的例外(至less不是我能看到的)。 有没有什么办法看到为什么SaveChanges抛出一个exception? 至less,看看SaveChanges在发生错误时试图更新哪个表是有帮助的。

当真正的例外似乎在某处丢失时,最好的办法就是打破每一个例外。 无论在哪里,在哪里,哪里,哪里,哪里,哪里,哪里,哪里,哪里,哪里,哪里,哪里,哪个地方,debugging器都会中断,让你看到发生了什么事情。

有关更多信息,请参阅此MSDN链接:

如何:抛出exception时中断

这是我的重写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; } } 

参考:

一个或多个实体的validation失败。 有关更多详细信息,请参阅“EntityValidationErrors”属性

这是我的重写SaveChanges,显示额外的代码来处理DbUpdateException(根据问题)。

  public override int SaveChanges() { try { return base.SaveChanges(); } catch (DbEntityValidationException vex) { var exception = HandleDbEntityValidationException(vex); throw exception; } catch(DbUpdateException dbu) { var exception = HandleDbUpdateException(dbu); throw exception; } } private Exception HandleDbUpdateException(DbUpdateException dbu) { var builder = new StringBuilder("A DbUpdateException was caught while saving changes. "); try { foreach (var result in dbu.Entries) { builder.AppendFormat("Type: {0} was part of the problem. ", result.Entity.GetType().Name); } } catch (Exception e) { builder.Append("Error parsing DbUpdateException: " + e.ToString()); } string message = builder.ToString(); return new Exception(message, dbu); } 

我没有使日志代码非常具体,但它改善了标准的错误信息,如:

 The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. 

这样,至less我可以看到哪个实体存在问题,而且这通常足以解决问题。

基于科林的回答,关于EF持久性失败的完整详细信息可以这样提供:

 public bool SaveChangesEx() { try { SaveChanges(); return true; } catch (DbEntityValidationException exc) { // just to ease debugging foreach (var error in exc.EntityValidationErrors) { foreach (var errorMsg in error.ValidationErrors) { // logging service based on NLog Logger.Log(LogLevel.Error, $"Error trying to save EF changes - {errorMsg.ErrorMessage}"); } } throw; } catch (DbUpdateException e) { var sb = new StringBuilder(); sb.AppendLine($"DbUpdateException error details - {e?.InnerException?.InnerException?.Message}"); foreach (var eve in e.Entries) { sb.AppendLine($"Entity of type {eve.Entity.GetType().Name} in state {eve.State} could not be updated"); } Logger.Log(LogLevel.Error, e, sb.ToString()); throw; } } 

除了validation错误,更新exception将输出一般错误和上下文信息。

注意:这个代码工作需要C#6.0,因为它使用了空传播和string插值。

我遇到同样的错误“将datetime2数据types转换为date时间数据types导致超出范围的值。\ r \ n该语句已被终止。

我把date时间值字段像这个Datetime.Now

 var person = new Person { FirstName = "Sebastian", LastName = "Back", **BirthDate = DateTime.Now,** IsActive = true, }; 

我得到了同样的错误,像这样解决;

我有一个数据types是date时间的字段,我知道我设置它没有分配的date时间属性。 所以我把它改为“ Datetime.Now ”,然后我看到问题解决了,并且已经保存成功了

在我的情况下,我使用存储过程来添加一个实体到数据库。 我的SP给运行时错误,因为我在C#中得到这个错误。 我纠正了SP和EF完美的工作。 因此,如果您正在使用SP在EF中添加,编辑,删除或更新,请检查相应的sp是否正常工作。