I have a batch work, I need to add new relations between existing entities, the entities have big data, I can’t get the entities and add them, I have tried this:
for .........
Class Class = new Class() { ClassID = myClassId };
context.AddToClasses(Class);
Student std = new Student() { Id = myStdId };
context.AddToStudents(std);
context.ObjectStateManager.ChangeObjectState(Class, System.Data.EntityState.Unchanged);
context.ObjectStateManager.ChangeObjectState(std, System.Data.EntityState.Unchanged);
Class.ClassStudents.Add(std);
.............
The first call will success, but the second one will give me this exception:
AcceptChanges cannot continue because the object’s key values conflict
with another object in the ObjectStateManager. Make sure that the key
values are unique before calling AcceptChanges.
Tried :
for .........
Class Class = new Class() { ClassID = myClassId };
context.Attach(Class);
Student std = new Student() { Id = myStdId };
context.Attach(std);
context.ObjectStateManager.ChangeObjectState(Class, System.Data.EntityState.Unchanged);
context.ObjectStateManager.ChangeObjectState(std, System.Data.EntityState.Unchanged);
Class.ClassStudents.Add(std);
.............
Exception:
An object with a null EntityKey value cannot be attached to an object
context.
How can I do this?
If the pairs of
myClassIdandmyStdIdcan be repetitive and in no particular order in the loop you are running over this code I would use two dictionaries to hold the already created and attached entities to avoid that you create and attach an entity with the same key a second time, like so: