You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The short description is that I get an exception when attempting to SaveChanges():
Test method EFTest.TestParentChildRelationship.AllAtOnce threw exception:
System.InvalidOperationException: A circular dependency was detected: 'Child' {'ParentParentId'} -> 'Parent' {'ParentId'}, 'Parent' {'FavoriteChildChildId'} -> 'Child' {'ChildId'}.
at Microsoft.Data.Entity.Internal.Multigraph2.BatchingTopologicalSort(Func2 formatCycle)
at Microsoft.Data.Entity.Update.Internal.CommandBatchPreparer.d__5.MoveNext()
at Microsoft.Data.Entity.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
at Microsoft.Data.Entity.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.Data.Entity.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
at EFTest.TestParentChildRelationship.AllAtOnce() in C:\Users\DHentschel\Documents\Visual Studio 2015\Projects\EFTest\EFTest\TestParentChildRelationship.cs:line 37
However, if I try to save the exact same data to the DB, but break the commit step into two staggered calls to SaveChanges(), then the data can be successfully saved, indicating that there isn't actually a circular dependency in either the schema or the data.
danBhentschel
The text was updated successfully, but these errors were encountered:
In your model you have two one-to-many relationships that go in opposite directions. A child belongs to a parent, and a parent has a favorite child. So there is a Parent.FavoriteChildId foreign key and a Child.ParentId foreign key.
So when you try to save, mark needs to be inserted first so that his key value can be referenced from the ParentId foreign key of the children. But the cyclic data arises because sally needs to be inserted before mark so that her key value can be referenced from the FavouriteChildId foreign key on mark.
I have created an extremely simple unit test project that illustrates this problem:
https://github.com/danBhentschel/EF7CircularDependencyTest
It was created using code-first and a SQLite DB.
The short description is that I get an exception when attempting to
SaveChanges()
:However, if I try to save the exact same data to the DB, but break the commit step into two staggered calls to
SaveChanges()
, then the data can be successfully saved, indicating that there isn't actually a circular dependency in either the schema or the data.The text was updated successfully, but these errors were encountered: