I'm using EF6 in my ASP.Net MVC application. For a particular table my view, update and delete code works but my insert throws a DbUpdateException
.
System.Data.Entity.Infrastructure.DbUpdateException was unhandled by user code
HResult=-2146233087
Message=Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Internal.InternalContext.SaveChanges() at SupportDiary.Models.SchedulerRequestService.Insert(WRequestViewModel request) in C:\Projects\SupportDiary\SupportDiary\Models\SchedulerRequestService.vb:line 92 at SupportDiary.Hubs.WRequestHub.Create(WRequestViewModel request) in C:\Projects\SupportDiary\SupportDiary\Hubs\WRequestHub.vb:line 37 at lambda_method(Closure , IHub , Object[] ) at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.Incoming(IHubIncomingInvokerContext context) InnerException: HResult=-2146233087 Message=Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values. Source=EntityFramework StackTrace: 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) at 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 options, Boolean executeInExistingTransaction) at System.Data.Entity.Internal.InternalContext.SaveChanges() InnerException:
This is the code for Insert and Update... as I said Update works.
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
If I drill into entity in the Insert code it as the Autonumber ID field (WRequestId) set to 0
I'm not sure how to fix this issue... searches on-line give details about a bug regarding DatabaseGeneratedOption.Identity but this looks like it was a bug in EF4. But I did check my edmx file and this parameter is set correctly on all my ID fields for each table.
This is the EF autogenerated file for the table in question...
'------------------------------------------------------------------------------
' <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
Any suggestions anyone?