摘要:使用 .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 时却不能?
任何建议将不胜感激。