# System.InvalidOperationException：导航属性 'X' 不是映射的实体类型。考虑使用 'DbContextOptionsBuilder.EnableSensitiveDataLogging' 来查看参数值。

- **ID:** `dotnet/ef-core-navigation-property-not-mapped`
- **领域:** dotnet
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

Entity Framework Core 尝试包含一个未注册为 DbSet 或缺少外键配置的导航属性。

## 版本兼容性

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

## 解决方案

1. ```
   确保导航属性的目标实体在 DbContext 中作为 DbSet 包含。例如：public DbSet<Order> Orders { get; set; }
   ```
2. ```
   在 OnModelCreating 中添加流式 API 配置：modelBuilder.Entity<Parent>().HasMany(p => p.Children).WithOne(c => c.Parent).HasForeignKey(c => c.ParentId);
   ```

## 无效尝试

- **** — This removes the navigation property entirely, breaking the relationship query. (70% 失败率)
- **** — Does not address the missing mapping; EF Core still cannot resolve the navigation. (80% 失败率)
