2016-06-08 23 views
0

我在我的ASP.Net MVC應用程序中使用EF6。對於特定的表格,我的視圖,更新和刪除代碼可以工作,但是我的插入引發了一個DbUpdateException實體框架6 - 使用自動編號ID創建記錄拋出DbUpdateException

System.Data.Entity.Infrastructure.DbUpdateException了未處理由用戶代碼
的HResult = -2146233087
消息=無法確定相關的操作的有效順序。由於外鍵約束,模型要求或商店生成的值,可能會存在依存關係。
源=的EntityFramework
堆棧跟蹤:
在System.Data.Entity.Internal.InternalContext.SaveChanges() 在SupportDiary.Models.SchedulerRequestService.Insert(WRequestViewModel請求)在C:\項目\ SupportDiary \ SupportDiary \模型\ SchedulerRequestService.vb:第92行 at SupportDiary.Hubs.WRequestHub.Create(WRequestViewModel request)in C:\ Projects \ SupportDiary \ SupportDiary \ Hubs \ WRequestHub.vb:line 37 at lambda_method(Closure,IHub,Object []) 在Microsoft.AspNet.SignalR.Hubs.HubDispatcher.Incoming(IHubIncomingInvokerContext上下文) InnerException: HResult = -2146233087 M essage =無法確定依賴操作的有效順序。由於外鍵約束,模型要求或商店生成的值,可能會存在依存關係。 源=的EntityFramework 堆棧跟蹤: 在System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.DependencyOrderingError(IEnumerable的1 remainder) at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ProduceCommands() at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func 1 FUNC,IDbExecutionStrategy executionStrategy,布爾startLocalTransaction,布爾releaseConnectionOnSuccess) 在System.Data.Entity.Core .Objects.ObjectContext.SaveChangesToStore(SaveOptions options,IDbExecutionStrategy executionStrategy,Boolean startLocalTransaction) at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute [TResult](Func`1 operation) at System.Data.Entity.Core.Objects。 ObjectContext.SaveChangesInternal(SaveOptions選項,布爾executeInExistingTransaction) at System.Data.Entity.Internal.InternalContext.SaveChanges() InnerException:

這是插入和更新的代碼......正如我所說的更新工作。

 Public Overridable Function Insert(request As WRequestViewModel) As WRequestViewModel 

     Dim entity As New tWorkRequest() 

     entity.Start = request.Start 
     entity.[End] = request.[End] 
     entity.Title = request.Title 
     entity.Diary = request.Diary 
     entity.Team = request.Team 
     entity.WorkManagerID = request.WorkManagerID 
     entity.AssigneeID = request.AssigneeID 
     entity.ChangeRef = request.ChangeRef 
     entity.Description = request.Description 
     entity.ImpactedServers = request.ImpactedServers 
     entity.ImpactedServices = request.ImpactedServices 
     entity.IsBAU = request.IsBAU 
     entity.ProjectRef = request.ProjectRef 
     entity.Notes = request.Notes 
     entity.IsOOH = request.IsOOH 
     entity.IsAllDay = request.IsAllDay 
     entity.RecurrenceRule = request.RecurrenceRule 
     entity.RecurrenceID = request.RecurrenceID 
     entity.RecurrenceException = request.RecurrenceException 
     entity.StartTimezone = request.StartTimezone 
     entity.EndTimezone = request.EndTimezone 
     entity.RequestStatus = request.RequestStatus 

     Using de As New SupportDiaryEntities 

      de.tWorkRequests.Add(entity) 
      de.SaveChanges() 

      request.WRequestID = entity.WRequestID 

      Return request 

     End Using 

    End Function 

    Public Overridable Sub Update(request As WRequestViewModel) 

     Using de As New SupportDiaryEntities 

      Dim entity = de.tWorkRequests.FirstOrDefault(Function(r) r.WRequestID = request.WRequestID) 

      entity.Start = request.Start 
      entity.[End] = request.[End] 
      entity.Title = request.Title 
      entity.Diary = request.Diary 
      entity.Team = request.Team 
      entity.WorkManagerID = request.WorkManagerID 
      entity.AssigneeID = request.AssigneeID 
      entity.ChangeRef = request.ChangeRef 
      entity.Description = request.Description 
      entity.ImpactedServers = request.ImpactedServers 
      entity.ImpactedServices = request.ImpactedServices 
      entity.IsBAU = request.IsBAU 
      entity.ProjectRef = request.ProjectRef 
      entity.Notes = request.Notes 
      entity.IsOOH = request.IsOOH 
      entity.IsAllDay = request.IsAllDay 
      entity.RecurrenceRule = request.RecurrenceRule 
      entity.RecurrenceID = request.RecurrenceID 
      entity.RecurrenceException = request.RecurrenceException 
      entity.StartTimezone = request.StartTimezone 
      entity.EndTimezone = request.EndTimezone 
      entity.RequestStatus = request.RequestStatus 

      de.SaveChanges() 

     End Using 

    End Sub 

如果我鑽到實體中插入代碼,它的自動編號ID字段(WRequestId)設置爲0

我不知道如何解決這個問題...搜索上線給有關與DatabaseGeneratedOption.Identity有關的錯誤的詳細信息,但這看起來像是EF4中的一個錯誤。但是我確實檢查了我的edmx文件,並且在每個表的所有ID字段中都正確設置了此參數。

這是有問題的表EF自動生成的文件...

'------------------------------------------------------------------------------ 
    ' <auto-generated> 
    '  This code was generated from a template. 
    ' 
    '  Manual changes to this file may cause unexpected behavior in your application. 
    '  Manual changes to this file will be overwritten if the code is regenerated. 
    ' </auto-generated> 
    '------------------------------------------------------------------------------ 

    Imports System 
    Imports System.Collections.Generic 

    Partial Public Class tWorkRequest 
     Public Property WRequestID As Integer 
     Public Property Title As String 
     Public Property Start As Date 
     Public Property [End] As Date 
     Public Property Diary As String 
     Public Property Team As String 
     Public Property WorkManagerID As Integer 
     Public Property AssigneeID As Integer 
     Public Property ChangeRef As String 
     Public Property Description As String 
     Public Property ImpactedServers As String 
     Public Property ImpactedServices As String 
     Public Property IsBAU As Boolean 
     Public Property ProjectRef As String 
     Public Property Notes As String 
     Public Property IsOOH As Boolean 
     Public Property IsAllDay As Boolean 
     Public Property RecurrenceRule As String 
     Public Property RecurrenceID As Nullable(Of Integer) 
     Public Property RecurrenceException As String 
     Public Property StartTimezone As String 
     Public Property EndTimezone As String 
     Public Property RequestStatus As Integer 

     Public Overridable Property tWorkRequests1 As ICollection(Of tWorkRequest) = New HashSet(Of tWorkRequest) 
     Public Overridable Property tWorkRequest1 As tWorkRequest 

    End Class 

任何建議任何人嗎?

+1

看看這個解決方案http://stackoverflow.com/questions/14153208/unable-to-determine-a-valid-ordering-for-dependent-operations – ssanga

回答

0

@ssanga指向的鏈接沒有可接受的解決方案,但將一般答案縫製爲將ID設置爲-1。我試過這個,但得到了另一個異常,指出另一個問題。該表的EF自動生成的類具有以下幾行...

Public Overridable Property tWorkRequests1 As ICollection(Of tWorkRequest) = New HashSet(Of tWorkRequest) 
Public Overridable Property tWorkRequest1 As tWorkRequest 

刪除這些行和我的創建工作。然後,我再次回到Visual Studio中的edmx文件和Generated Model for Database中,這些行不再出現。

不知道爲什麼他們在那裏擺在首位。