System.InvalidOperationException: A navigation property 'NavigationName' is null. Related data must be loaded using Include or other eager loading methods.
ID: dotnet/ef-core-navigation-property-null-after-include
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 6.0 | active | — | — | — |
| 7.0 | active | — | — | — |
| 8.0 | active | — | — | — |
| 9.0 | active | — | — | — |
Root Cause
Entity Framework Core navigation property is null because related data was not eagerly loaded with .Include() or .ThenInclude() before accessing it after a query.
generic中文
Entity Framework Core 导航属性为 null,因为在查询后未使用 .Include() 或 .ThenInclude() 预先加载相关数据。
Official Documentation
https://learn.microsoft.com/en-us/ef/core/querying/related-data/eagerWorkarounds
-
95% success Add .Include() with .ThenInclude() for nested navigation properties. Example: var orders = context.Orders.Include(o => o.Customer).ThenInclude(c => c.Address).ToList();
Add .Include() with .ThenInclude() for nested navigation properties. Example: var orders = context.Orders.Include(o => o.Customer).ThenInclude(c => c.Address).ToList();
-
85% success Use explicit loading with .Collection().Load() or .Reference().Load() after the query. Example: var order = context.Orders.First(); context.Entry(order).Collection(o => o.OrderItems).Load();
Use explicit loading with .Collection().Load() or .Reference().Load() after the query. Example: var order = context.Orders.First(); context.Entry(order).Collection(o => o.OrderItems).Load();
-
90% success Enable lazy loading by installing Microsoft.EntityFrameworkCore.Proxies and configuring: optionsBuilder.UseLazyLoadingProxies(); then ensure navigation properties are virtual.
Enable lazy loading by installing Microsoft.EntityFrameworkCore.Proxies and configuring: optionsBuilder.UseLazyLoadingProxies(); then ensure navigation properties are virtual.
中文步骤
添加 .Include() 和 .ThenInclude() 以加载嵌套导航属性。示例:var orders = context.Orders.Include(o => o.Customer).ThenInclude(c => c.Address).ToList();
查询后使用 .Collection().Load() 或 .Reference().Load() 进行显式加载。示例:var order = context.Orders.First(); context.Entry(order).Collection(o => o.OrderItems).Load();
通过安装 Microsoft.EntityFrameworkCore.Proxies 并配置启用延迟加载:optionsBuilder.UseLazyLoadingProxies(); 确保导航属性为 virtual。
Dead Ends
Common approaches that don't work:
-
Adding .Include() to the query but missing .ThenInclude() for nested navigation properties
75% fail
EF Core requires explicit .ThenInclude() for second-level navigation properties; only Include() loads the immediate property but not its children.
-
Using .Load() after the query without calling .Collection() or .Reference() correctly
60% fail
Explicit loading with .Load() requires specifying the navigation property via .Collection() or .Reference() first; incorrect syntax leads to same null error.
-
Setting LazyLoadingEnabled = true in DbContext but not installing the Microsoft.EntityFrameworkCore.Proxies package
80% fail
Lazy loading requires both configuration and the Proxies NuGet package; missing the package causes no automatic loading and null navigation properties.