Frameworks EF代码优先:更新实体

Frameworks EF代码优先:更新实体,frameworks,entity,Frameworks,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

我拥有以下实体:

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
插入新实体效果良好。但是第二次使用相同的WF对象进行更新时,我遇到了以下错误:

保存未公开其关系的外键属性的实体时出错。EntityEntries属性将返回null,因为无法将单个实体标识为异常源。通过在实体类型中公开外键属性,可以更轻松地在保存时处理异常。有关详细信息,请参见InnerException

bug在哪里?

我曾经遇到过这种情况。 我想我只是通过暴露EF要求的钥匙就解决了这个问题 (公开密钥意味着持有引用的实体还具有包含外键的属性) e、 g:

这将在DB

类似问题中创建FK限制。
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
public class Holder
{
    public Held HeldObject{get;set;}
    public int HeldID //this is the primary key for the entity Held (exact same name)
}