I've the following entity:
Partial Public Class Workflow
Sub New()
Activities = New List(Of WFActivity)
End Sub
<Key()>
Public Property ID As Long
Public Property Customer As Customer
<Required(), MaxLength(100)>
Public Property Name As String
Public Property Activities As List(Of WFActivity)
End Class
To add and update an entity I use the following procedure:
Public Sub SaveWorkflow(ByVal WF As Workflow)
Dim wfa As WFActivity
Try
Using ctx = New MyContext
ctx.Workflow.Add(WF)
If WF.ID > 0 Then
ctx.Entry(WF).State = EntityState.Modified
End If
For Each wfa In WF.Activities
If wfa.ID > 0 Then
ctx.Entry(wfa).State = EntityState.Modified
End If
Next
If WF.Customer.ID > 0 Then
ctx.Entry(WF.Customer).State = EntityState.Modified
End If
ctx.SaveChanges()
End Using
Catch ex As Exception
End Try
End Function
Inserting a new entity works fine. But using the same WF object for update purpose a second time with this procedure I got the following error:
An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.
Where is the bug?