EF 3.1 why i lost the collection items on other entity when i am adding to a collection

17 Views Asked by At

i have a child object which could be on more than one parents collection navigation property. when i add entities to one parent, they are removed from other parents collection.

Example:

class A {
 ICollection<B> Children {get;set;}
}
class B {...}

A objA = new A()...
A objA2 = new A()...
B objB = new B()...
...

objA.Children.Add(objB);
dbContext.DbSetOfA.Add(objA);
objA2.Children.Add(objB);

// all good until now,,
// with next line we will lose the instance objB in objA.Children
dbContext.DbSetOfA.Add(objA2);

i understand EF tracks the entities and try to keep object is contained in only one collection. But why?? and how to avoid this ?

i tried to define a many-to-many relation but it is not supported in EF 3. Try to use not mapped but this time we are not able to use the property "Children" in linq.

0

There are 0 best solutions below