dotnet
data_error
ai_generated
true
System.InvalidOperationException: The navigation property 'X' is not a mapped entity type. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the parameter values.
ID: dotnet/ef-core-navigation-property-not-mapped
85%Fix Rate
88%Confidence
1Evidence
2023-06-15First 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
Entity Framework Core attempts to include a navigation property that is not registered as a DbSet or has missing foreign key configuration.
generic中文
Entity Framework Core 尝试包含一个未注册为 DbSet 或缺少外键配置的导航属性。
Official Documentation
https://learn.microsoft.com/en-us/ef/core/querying/related-dataWorkarounds
-
90% success Ensure the navigation property's target entity is included in the DbContext as a DbSet. For example: public DbSet<Order> Orders { get; set; }
Ensure the navigation property's target entity is included in the DbContext as a DbSet. For example: public DbSet<Order> Orders { get; set; } -
85% success Add fluent API configuration in OnModelCreating: modelBuilder.Entity<Parent>().HasMany(p => p.Children).WithOne(c => c.Parent).HasForeignKey(c => c.ParentId);
Add fluent API configuration in OnModelCreating: modelBuilder.Entity<Parent>().HasMany(p => p.Children).WithOne(c => c.Parent).HasForeignKey(c => c.ParentId);
中文步骤
确保导航属性的目标实体在 DbContext 中作为 DbSet 包含。例如:public DbSet<Order> Orders { get; set; }在 OnModelCreating 中添加流式 API 配置:modelBuilder.Entity<Parent>().HasMany(p => p.Children).WithOne(c => c.Parent).HasForeignKey(c => c.ParentId);
Dead Ends
Common approaches that don't work:
-
70% fail
This removes the navigation property entirely, breaking the relationship query.
-
80% fail
Does not address the missing mapping; EF Core still cannot resolve the navigation.