0

摘要:使用 .attachto 的多个多对多关系添加工作正常,但相同的代码,除了是 .remove,而不是 .add,会导致可怕的:

ObjectStateManager 中已存在具有相同键的对象。ObjectStateManager 无法跟踪具有相同键的多个对象。

所以这里是代码。根据表单字段,一个隐藏(与之前的培训师值),一个不,我循环浏览培训师列表以查看已选择或取消选择哪些培训师,然后添加或删除客户端和之间的多对多关系培训师:

Protected Sub dsClient_Updated(sender As Object, e As System.Web.UI.WebControls.EntityDataSourceChangedEventArgs) Handles dsClient.Updated
    Dim Client As Client = e.Entity
    Dim Trainers As IEnumerable(Of aspnet_Users), CurrentTrainer As String, PrevTrainer As String

    ' Select list of Trainers
    Trainers = From S In FitnessDB.aspnet_Users

    For Each Trainer As aspnet_Users In Trainers
        CurrentTrainer = Request.Form(Trainer.UserName)
        PrevTrainer = Request.Form("Prev_" & Trainer.UserName)
        ' Check if Trainer has been deleted
        If CurrentTrainer = "" And PrevTrainer <> "" Then
            ' Trainer previously existed, delete
            FitnessDB.Detach(Trainer)
            e.Context.AttachTo ("aspnet_Users", Trainer)
            Client.aspnet_Users.Remove (Trainer)
        ElseIf CurrentTrainer <> "" And PrevTrainer = "" Then
            ' Doesn't exist, add
            FitnessDB.Detach(Trainer)
            e.Context.AttachTo ("aspnet_Users", Trainer)
            Client.aspnet_Users.Add (Trainer)
        End If
    Next
    e.Context.SaveChanges()
End Sub

我可以选择多个培训师来添加就好了。它循环通过:

FitnessDB.Detach(Trainer)
e.Context.AttachTo ("aspnet_Users", Trainer)
Client.aspnet_Users.Add (Trainer)

没有问题,添加新的关系就好了。如果我只删除一个教练,它工作正常。但是,如果我尝试使用下面的代码删除多个培训师

FitnessDB.Detach(Trainer)
e.Context.AttachTo ("aspnet_Users", Trainer) <-- Error: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
Client.aspnet_Users.Remove (Trainer)

然后我在第二次删除尝试时收到错误。

那么令我困惑的是:为什么我可以在.add'ing 时成功.attachto 多个“trainer”,但在.remove'ing 时却不能?

任何建议将不胜感激。

4

1 回答 1

1

在这一点上,我已经初步得出结论,这是实体框架中的一个错误,原因有很多,我太累了,无法在这里详细介绍。

然而,我想出了一个解决方法。我删除了上面检查培训师删除的代码部分,并将其替换为:

For Each Trainer As aspnet_Users In Client.aspnet_Users
    If Request.Form(Trainer.UserName) = "" Then DeleteTrainers.Add (Trainer)
Next
For Each Trainer As aspnet_Users In DeleteTrainers
    Client.aspnet_Users.Remove (Trainer)
Next

重写的整个子现在是:

Protected Sub dsClient_Updated(sender As Object, e As System.Web.UI.WebControls.EntityDataSourceChangedEventArgs) Handles dsClient.Updated
    Dim Client As Client = e.Entity
    Dim DeleteTrainers As New List(Of aspnet_Users), CurrentTrainer As String, PrevTrainer As String

    ' Check for trainers that have been removed
    For Each Trainer As aspnet_Users In Client.aspnet_Users
        If Request.Form(Trainer.UserName) = "" Then DeleteTrainers.Add (Trainer)
    Next
    For Each Trainer As aspnet_Users In DeleteTrainers
        Client.aspnet_Users.Remove (Trainer)
    Next

    ' Check for trainers to add
    For Each Trainer As aspnet_Users In FitnessDB.aspnet_Users
        CurrentTrainer = Request.Form(Trainer.UserName)
        PrevTrainer = Request.Form("Prev_" & Trainer.UserName)
        If CurrentTrainer <> "" And PrevTrainer = "" Then
            ' Doesn't exist, add
            FitnessDB.Detach(Trainer)
            e.Context.AttachTo ("aspnet_Users", Trainer)
            Client.aspnet_Users.Add (Trainer)
        End If
    Next
    e.Context.SaveChanges()
End

通过转向使用 Client.aspnet_Users,我能够留在相同的上下文(e.context)中,从而消除了必须从 FitnessDB 上下文分离和重新附加到 e.context 的问题,然后当我崩溃时删除实体 Trainer。

通过首先循环遍历相关培训师的表并将它们添加到列表中,然后我可以通过循环遍历列表来成功删除它们(我不能直接在表循环中删除它们,因为这会使循环出错“集合已修改;枚举操作可能无法执行。”。

希望有一天这对某人有所帮助。

于 2013-08-23T03:11:17.173 回答