'EF Core 6 - System.InvalidOperationException: Unable to save changes because a circular dependency was detected in the data to be saved
for the sake of simplicity, let say I have to projects:
- WebAPI and
- UnitTest for WebAPI
Both projects were using EF Core 5. WebAPI uses SqlServer and UnitTest uses the Sqlite (inMemory). Both projects work correctly.
I migrated from EF Core 5 to EF Core 6 and now WebAPI works correctly but I can't execute any of my Unit Tests. I get the following error:
System.InvalidOperationException: Unable to save changes because a circular dependency was detected in the data to be saved: 'Microsoft.EntityFrameworkCore.Migrations.Operations.CreateTableOperation -> Microsoft.EntityFrameworkCore.Migrations.Operations.CreateTableOperation -> Microsoft.EntityFrameworkCore.Migrations.Operations.CreateTableOperation -> Microsoft.EntityFrameworkCore.Migrations.Operations.CreateTableOperation'.. at Microsoft.EntityFrameworkCore.Utilities.Multigraph
2.ThrowCycle(List1 cycle, Func2 formatCycle, Func2 formatException) at Microsoft.EntityFrameworkCore.Utilities.Multigraph2.TopologicalSort(Func4 tryBreakEdge, Func2 formatCycle, Func2 formatException) at Microsoft.EntityFrameworkCore.Utilities.Multigraph2.TopologicalSort(Func4 tryBreakEdge) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target) at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.GetCreateTablesCommands(MigrationsSqlGenerationOptions options) at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.CreateTables() at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated() at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()
The circular reference seems to happen inside Microsoft.EntityFrameworkCore.Migrations.Operations.CreateTableOperation and since the WebAPI project works, I assume that this is a problem with the Sqlite provider.
The error seems to happen when the EF Configurations are executed inside the OnModelCreating method of DbContext. At this point, no attempted to save/update and entity has been made. The unit test fails during the EF configuration.
At this point I am clueless about how to proceed. We have around 100 configurations and there is no more information provided in the exception. Since everything was working on EF Core 5, I am assuming it is either a breaking change (specific to Sqllite) or a bug in EF Core 6.
Has anybody run into this issue? o does anybody have any ideas about how to figure out what the cause of the problem is?
Any help is highly appreciated.
Solution 1:[1]
This has been fixed in version 6.0.2 I can confirm.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Micah Osborne |
