msawczyn / EFDesigner2022

Entity Framework visual design surface and code-first code generation for EF6, Core and beyond
MIT License
119 stars 21 forks source link

Raising Error "The object has been removed from the model." after calling IsRequired() on a Navigation for a modelbuilder entity. #4

Closed Kalle4242 closed 2 years ago

Kalle4242 commented 2 years ago

When creating an empty database from the generated modelbuilder code an error is raised: "The object has been removed from the model." after calling the IsRequired() method on the navigation builder.

modelBuilder.Entity<global::StrategyCompass.QuickTools.Db.AssetsDb.Model.Entities.AssetCommonDbo>() .HasMany<global::StrategyCompass.QuickTools.Db.AssetsDb.Model.Entities.SubscriberDbo>(p => p.Subscribers) .WithOne(p => p.AssetCommon) .HasForeignKey("AssetCommonId"); modelBuilder.Entity<global::StrategyCompass.QuickTools.Db.AssetsDb.Model.Entities.SubscriberDbo>().Navigation(e => e.AssetCommon).IsRequired(); This issue already occured when bringing the generated code from VS2019/NET5.0 to VS2022/NET6.0 but now it occurs as well after generating the code with the freshly installed rc2 (4.0.0.3). It occurs for all bidirectional one-to-many relations in the model.

In the meantime I've helped myself with moving the IsRequired() method call to the "HasForeignKey" ReferenceCollectionBuilder in the code line before. This worked for a start, but I'm not shure if does same.

As well I'm not shure, if this issue depends on EF Core issue https://github.com/dotnet/efcore/issues/26611 and will be fixed with EF Core 6.0.1 Thanks.