【问题标题】:Entity Framework 6 - Creating record with Autonumber ID throws DbUpdateException实体框架 6 - 使用自动编号 ID 创建记录会引发 DbUpdateException
【发布时间】:2016-06-08 14:59:35
【问题描述】:

我在我的 ASP.Net MVC 应用程序中使用 EF6。对于特定表,我的视图、更新和删除代码有效,但我的插入会抛出 DbUpdateException

System.Data.Entity.Infrastructure.DbUpdateException 未被用户代码处理
HResult=-2146233087
消息=无法确定相关操作的有效排序。由于外键约束、模型要求或存储生成的值,可能存在依赖关系。
来源=实体框架
堆栈跟踪:
在 System.Data.Entity.Internal.InternalContext.SaveChanges() 在 SupportDiary.Models.SchedulerRequestService.Insert(WRequestViewModel request) 在 C:\Projects\SupportDiary\SupportDiary\Models\SchedulerRequestService.vb:line 92 在 SupportDiary.Hubs.WRequestHub.Create(WRequestViewModel request) 在 C:\Projects\SupportDiary\SupportDiary\Hubs\WRequestHub.vb:line 37 在 lambda_method(闭包,IHub,对象 []) 在 Microsoft.AspNet.SignalR.Hubs.HubDispatcher.Incoming(IHubIncomingInvokerContext 上下文) 内部异常: H结果=-2146233087 消息=无法确定相关操作的有效排序。由于外键约束、模型要求或存储生成的值,可能存在依赖关系。 来源=实体框架 堆栈跟踪: 在 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](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) 在 System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions 选项,IDbExecutionStrategy executionStrategy,布尔 startLocalTransaction) 在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 操作) 在 System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions 选项,布尔型 executeInExistingTransaction) 在 System.Data.Entity.Internal.InternalContext.SaveChanges() 内部异常:

这是插入和更新的代码...正如我所说的更新有效。

        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

大家有什么建议吗?

【问题讨论】:

标签: vb.net entity-framework


【解决方案1】:

@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,这些行没有再次出现。

不知道为什么他们一开始就在那里。

【讨论】:

    猜你喜欢
    • 2011-03-02
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多