Example:
Person => Child
Child => Person
Tables:
Person
Child -> PersonID FK
PersonChildren -> PersonID / ChildID
Adding Child to Person creates a new duplicate entry in the child table for each parent person.
...
IList<Person> people = new IList<Person> { ... }
foreach (Person curPerson in people)
{
curPerson.Children = new IList<Child> { ... }
context.Person.AddOrUpdate(curPerson);
}
context.SaveChanges();
...
between the elipses I am adding the specific information for each class, which works. The result is table Person containing 2 entries (as expected) and the child table consisting of 4 (2 are expected). Two children to two parents, same two children have the same two parents (mom and dad in this case). The join table has the 4 entries that are also expected ({PersonID=1, ChildID=1}, {PersonID=1, ChildID=2} .... )
I have tried this with finding the ID of the object that I want to add to parent
... curPerson.Children = dbRepo.Find(child => child.ID == 1); context.Person.AddOrUpdate(curPerson); ...
This causes error
System.InvalidOperationException: An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
when running the seed method.
I am using update-database -verbose when migrating new changes instead of relying on database migration on load.

New Topic/Question
Reply


MultiQuote



|