I have an entity framework project setup. It it I have four tables and three entities ("A", "B", & "C"). There is a 1:n relationship between A and B, and between A and C, and a n:n relationship between B and C.
I use the following code to try to add a new "B" entity:
A t = null;
if (this.MyA == null)
{
t = new A()
{
EntityKey = this.MyAReference.EntityKey,
AId = (Guid)this.MyAReference.EntityKey.EntityKeyValues[0].Value
};
}
else
{
t = this.MyA;
}
this.MyA = null;
context.Attach(t);
this.MyA = t;
The "B" object ("this") has a child collection of "C" objects. These "C" objects already exist in the database, but are not related to the new object (obviously). When I call "SaveChanges()", it throws an exception because it tries to add all of the child "C" objects an new objects in the database.
How can I get it to just create the reference in the "Table_JoinBC", and not try to recreate the "C" objects?
Thanks for any help.