I am trying to save many tables in one operation.
T1 many to many relation with T3,(T2 being used as the association table).
T3 has One to Many relation with T4
T4 has one to one relation with T5
T3 has one to many relation with T6
Now, I created all the objects and I try to persist T1 hoping all the relations will be persisted too. _service.Create(T1)
Unfortunately I got an error: "Unable to determine a valid ordering for dependent
operations. Dependencies may exist due to foreign key constraints, model requirements,
or store-generated values."
I tried to save the data in two phases: Save T3 (with T4, T5 and T6) Then get the id of T3 and assign to T2 create T1 and add T2 to it Save T1
But I still get the same error. Can anyone point out what i am doing wrong? I want to keep it in 1-2 ops to be able to rollback if ever i get an error
Edit 1: T3 has One to Many relation with T4
I think that it's the 1-1 relationship that's causing the error. If I omit the object and I save T1 (which contains T2, T3, T4 and T6), then all objects are persisted to the DB.
So i think that, arriving at relation T4, EF cannot decide which way to go first. I would expect it to save T5 first, since, being a 1-1 relation it needs to save T5 and use the Id as FK in T4. Then I would expect EF to save T4 and use the Id to save T5.
But that's not the case, EF seems stuck at T4, hence the error.
As a test, I made T4-T5 a one-to-many relation (instead of a 1-1), then if I persist T1 (which now contains T2, T3, T4, T5 and T6), all relations are saved correctly.