Given the entities,
class A {
B DefaultB { get; set; }
C DefaultC { get; set; }
}
class B {
A { get; set; }
}
class C {
A { get; set; }
}
where A.DefaultB and A.DefaultC are optional,
modelBuilder.Entity<A>().HasOptional(x => x.DefaultB).WithMany();
modelBuilder.Entity<A>().HasOptional(x => x.DefaultC).WithMany();
what combination of context.Set<T>().Add(...) and context.SaveChanges() would work to save the following object graph?
var b = new B();
var c = new C();
var a = new A { DefaultB = b, DefaultC = c, };
b.A = a;
c.A = a;
// now save these...
As it is now, I get an InvalidOperationException (collection modification while iterating) because EF does not seem to handle cycles very well.
Hi you need to adjust your mapping:
And the correct save order with this mapping: