dotnet
config_error
ai_generated
true
System.InvalidOperationException: Cannot create a DbSet for 'X' because a DbSet with the same entity type already exists.
ID: dotnet/ef-core-multiple-navigation-sets
90%Fix Rate
90%Confidence
1Evidence
2023-05-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| EF Core 6.0 | active | — | — | — |
| EF Core 7.0 | active | — | — | — |
| EF Core 8.0 | active | — | — | — |
Root Cause
The DbContext class defines two or more DbSet properties that map to the same entity type, causing ambiguity in EF Core's internal model building.
generic中文
DbContext 类定义了两个或更多映射到同一实体类型的 DbSet 属性,导致 EF Core 内部模型构建产生歧义。
Official Documentation
https://learn.microsoft.com/en-us/ef/core/modeling/entity-typesWorkarounds
-
95% success Remove the duplicate DbSet property from the DbContext class. Keep only one declaration for the entity type. For example, remove either 'public DbSet<Order> Orders { get; set; }' or 'public DbSet<Order> SalesOrders { get; set; }'.
Remove the duplicate DbSet property from the DbContext class. Keep only one declaration for the entity type. For example, remove either 'public DbSet<Order> Orders { get; set; }' or 'public DbSet<Order> SalesOrders { get; set; }'. -
80% success If you need multiple query surfaces, use separate DbContext classes for different bounded contexts.
If you need multiple query surfaces, use separate DbContext classes for different bounded contexts.
中文步骤
从 DbContext 类中移除重复的 DbSet 属性。只保留一个实体类型声明。例如,移除 'public DbSet<Order> Orders { get; set; }' 或 'public DbSet<Order> SalesOrders { get; set; }' 中的一个。如果需要多个查询接口,为不同的限界上下文使用单独的 DbContext 类。
Dead Ends
Common approaches that don't work:
-
70% fail
This removes the property from the context entirely, losing the ability to query that entity through that property.
-
50% fail
This duplicates code and does not solve the core issue of duplicate DbSet definitions.