Hi i am trying to attach multiple objects in ObjectContextManager and try to set their state in “Added” Mode. These objects have some navigational properties with them and when i try to attach any object after attaching the first object. It throws me the error
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
The reason seems to be very obvious but what will be its solution.
Kindly guide
You should initialize the Id of new entities you add/attach with a random Id (like
Guid.NewGuid().GetHashCode()if you use Int32 as Id-type)Otherwise have all entities the default Id of 0, and so will the ObjectStateManager throw an exception when you attach the entities.