# System.InvalidOperationException：无法为 'X' 创建 DbSet，因为已存在具有相同实体类型的 DbSet。

- **ID:** `dotnet/ef-core-multiple-navigation-sets`
- **领域:** dotnet
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

DbContext 类定义了两个或更多映射到同一实体类型的 DbSet 属性，导致 EF Core 内部模型构建产生歧义。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| EF Core 6.0 | active | — | — |
| EF Core 7.0 | active | — | — |
| EF Core 8.0 | active | — | — |

## 解决方案

1. ```
   从 DbContext 类中移除重复的 DbSet 属性。只保留一个实体类型声明。例如，移除 'public DbSet<Order> Orders { get; set; }' 或 'public DbSet<Order> SalesOrders { get; set; }' 中的一个。
   ```
2. ```
   如果需要多个查询接口，为不同的限界上下文使用单独的 DbContext 类。
   ```

## 无效尝试

- **** — This removes the property from the context entirely, losing the ability to query that entity through that property. (70% 失败率)
- **** — This duplicates code and does not solve the core issue of duplicate DbSet definitions. (50% 失败率)
