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
{{ message }}
Repository navigation
Unexpected "The object has been removed from the model." triggered by specific nesting and property naming in CosmosDb Provider #34329
Today I ran into a weird InvalidOperationException with message "The object has been removed from the model.". And it took me hours to narrow down what triggers the exception.
It turns out that when objects are nested in a specific pattern and with some uncertain property naming, the exception emerges. And the exception is specific to CosmosDb provider. It could be reproduced as (.NET Fiddle is also available):
usingMicrosoft.EntityFrameworkCore;// using var cxt = new SqlServerCxt(); // sql server provider is fineusingvarcxt=newCosmosDbCxt();cxt.Find<EntityType1>();// trigger building of the dbcontextpublicclassCosmosDbCxt:DbContext{publicDbSet<EntityType1>EntityType1{get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder){optionsBuilder.UseCosmos(// you can stick with the fake values, the exception will be thrown prior to the connection"<edpt>","<key>","<dbName>");}protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.Entity<EntityType1>().Ignore(x =>x.E2);// exception thrown here}}// sql server provider is fine// public class SqlServerCxt : DbContext// {// public DbSet<EntityType1> EntityType1 { get; set; }// protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)// {// optionsBuilder.UseSqlServer("Server=.");// }// protected override void OnModelCreating(ModelBuilder modelBuilder)// {// modelBuilder.Entity<EntityType1>().Ignore(x => x.E2);// }// }publicclassEntityType1{publicEntityType2E2{get;set;}}publicclassEntityType2{publicEntityType3E3{get;set;}publicEntityType4E4{get;set;}}publicclassEntityType3{publicEntityType2U2{get;set;}// changing the property name here will either fix or break the issue, name like 'E2', 'A' is fine, but name like 'U2', 'Exy' is not. I could not find a pattern here}publicclassEntityType4{publicEntityType3?E3{get;set;}}
EF Core version: 8.0.7
Database provider: Microsoft.EntityFrameworkCore.Cosmos
Target framework: NET 8.0
Operating system: Windows 11 Pro 23H2 Build 22631.3958
Activity
AndriySvyryd commentedon Aug 9, 2024
This will be fixed in 9.0, but we don't think this affects enough users to meet our patch bar.
As a workaround you can ignore the target entity type first:
If it still should be refenced from other entity types you can configure those navigations explicitly afterwards.
Don't remove non-existant foreign keys.
[release/9.0] Don't remove non-existent foreign keys. (#34614)