我在ASP.Net MVC应用程序中使用EF6。对于特定的表我的视图,更新和删除代码有效,但我的插入会抛出DbUpdateException
。
用户代码未处理System.Data.Entity.Infrastructure.DbUpdateException 的HResult = -2146233087
Message =无法确定相关操作的有效排序。由于外键约束,模型要求或存储生成的值,可能存在依赖关系 来源=的EntityFramework
堆栈跟踪:
在System.Data.Entity.Internal.InternalContext.SaveChanges() 在C:\ Projects \ SupportDiary \ SupportDiary \ Models \ SchedulerRequestService.vb中的SupportDiary.Models.SchedulerRequestService.Insert(WRequestViewModel请求):第92行 在C:\ Projects \ SupportDiary \ SupportDiary \ Hubs \ WRequestHub.vb中的SupportDiary.Hubs.WRequestHub.Create(WRequestViewModel请求):第37行 在lambda_method(Closure,IHub,Object []) 在Microsoft.AspNet.SignalR.Hubs.HubDispatcher.Incoming(IHubIncomingInvokerContext context) 的InnerException: 的HResult = -2146233087 Message =无法确定相关操作的有效排序。由于外键约束,模型要求或存储生成的值,可能存在依赖关系。 来源=的EntityFramework 堆栈跟踪: at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.DependencyOrderingError(IEnumerable1 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,Boolean startLocalTransaction,Boolean releaseConnectionOnSuccess) 在System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions选项,IDbExecutionStrategy executionStrategy,Boolean startLocalTransaction) 在System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute [TResult](Func`1操作) at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options,Boolean executeInExistingTransaction) 在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
有人建议吗?
答案 0 :(得分: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文件和数据库的生成模型,这些行没有重新出现。
不确定他们为什么一开始就在那里。