开发者

How do I prevent InvalidOperationException saving a cyclic graph of entities?

开发者 https://www.devze.com 2023-04-13 07:47 出处:网络
Given the entities, class A { B DefaultB { get; set; } C DefaultC { get; set; } } class B { A { get; set; }

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 ge开发者_StackOverflow社区t an InvalidOperationException (collection modification while iterating) because EF does not seem to handle cycles very well.


Hi you need to adjust your mapping:

modelBuilder.Entity<A>().HasOptional(a => a.DefaultB);
modelBuilder.Entity<A>().HasOptional(m => m.DefaultC);
modelBuilder.Entity<B>().HasRequired(m => m.A);
modelBuilder.Entity<C>().HasRequired(m => m.A);

And the correct save order with this mapping:

var a = new A() { Id = 3 };
context.As.Add(a);
context.SaveChanges();

var b = new B() { Id = 1};
var c = new C() { Id = 2 };
context.Bs.Add(b);
context.Cs.Add(c);
b.A = a;
c.A = a;
a.DefaultB = b;
a.DefaultC = c;
context.SaveChanges();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号