--here was wrong model without association manyTOmany between A-B. corrected is in EDIT2--
A
exists in database, B
exists in database.
I need only enter new C
element with some Properties1 and Properties2 (and update collections of C
in existed A
and B
elements)
I tried many options, like for example this, but still somethings wrong (with ObjectOCntext and existed Key etc)
void SaveNewC(C newC)
{
using (var context = new MyEntities(connectionString))
{
var dbA = context.A.Where(a => a.Id == newC.A.Id).SingleOrDefault();
var dbB = context.B.Where(b => b.Id == newC.B.Id).SingleOrDefault();
newC.A = dbA;
newC.B = dbB;
context.AddObject(newC);
context.SaveChanges();
}
}
EDIT
Exception that I get: "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."
EDIT2 updated model