私はエンティティA
とB
。
A
フィールドがあります:
id
(PK)およびb_fk
(FK to B's id
)。
B
同様のフィールドがあります:
id
(PK)とa_fk
(FK to A's id
)。
A
今、私はオブジェクトを作成したいと思いますB
:
createAAndBMethod(Entities context){
A a = new A();
B b = new B();
a.b_fk = b;
b.a_fk = a;
context.As.AddObject(a);
context.Bs.AddObject(b);
}
someImportantMethod(){
//do sth
Entities context = new Entities(...);
//do sth, maybe some changes to db on context
createAAndBMethod(context);
//do sth, maybe some changes to db on context
context.SaveChanges();// <-- here I'm getting error
}
保存はエラーで機能しません:Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
1回の保存でこれを機能させる方法はありますか?変更を保存してはならないコードで両方のオブジェクトを作成する必要があるため、context.SaveChanges()
以前はどこでも実行できません。
context.SaveChanges()
次のようなことが起こる可能性があります。
Create newA with nulled field b_fk
Create newB with a_fk = newA
Set newA.b_fk to newB
更新:とは両方ともa_fk
nullb_fk
許容です。私はMSSQLとAzureSQLを使用しています。
Update2:次のように変更createAAndBMethod(Entities context)
しました:
createAAndBMethod(Entities context){
A a = new A();
context.As.AddObject(a);
B b = new B();
a.b_fk = b;
}
しかし、それでも同じエラーでは機能しません。