{
  "id": "dotnet/ef-core-navigation-property-null-after-include",
  "signature": "System.InvalidOperationException: A navigation property 'NavigationName' is null. Related data must be loaded using Include or other eager loading methods.",
  "signature_zh": "System.InvalidOperationException: 导航属性 'NavigationName' 为 null。必须使用 Include 或其他预先加载方法加载相关数据。",
  "regex": "System\\.InvalidOperationException: A navigation property '.*' is null\\. Related data must be loaded using Include or other eager loading methods\\.",
  "domain": "dotnet",
  "category": "data_error",
  "subcategory": null,
  "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.",
  "root_cause_type": "generic",
  "root_cause_zh": "Entity Framework Core 导航属性为 null，因为在查询后未使用 .Include() 或 .ThenInclude() 预先加载相关数据。",
  "versions": [
    {
      "version": "6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "9.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Adding .Include() to the query but missing .ThenInclude() for nested navigation properties",
      "why_fails": "EF Core requires explicit .ThenInclude() for second-level navigation properties; only Include() loads the immediate property but not its children.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using .Load() after the query without calling .Collection() or .Reference() correctly",
      "why_fails": "Explicit loading with .Load() requires specifying the navigation property via .Collection() or .Reference() first; incorrect syntax leads to same null error.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "Setting LazyLoadingEnabled = true in DbContext but not installing the Microsoft.EntityFrameworkCore.Proxies package",
      "why_fails": "Lazy loading requires both configuration and the Proxies NuGet package; missing the package causes no automatic loading and null navigation properties.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Add .Include() with .ThenInclude() for nested navigation properties. Example: var orders = context.Orders.Include(o => o.Customer).ThenInclude(c => c.Address).ToList();",
      "success_rate": 0.95,
      "how": "Add .Include() with .ThenInclude() for nested navigation properties. Example: var orders = context.Orders.Include(o => o.Customer).ThenInclude(c => c.Address).ToList();",
      "condition": "",
      "sources": []
    },
    {
      "action": "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();",
      "success_rate": 0.85,
      "how": "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();",
      "condition": "",
      "sources": []
    },
    {
      "action": "Enable lazy loading by installing Microsoft.EntityFrameworkCore.Proxies and configuring: optionsBuilder.UseLazyLoadingProxies(); then ensure navigation properties are virtual.",
      "success_rate": 0.9,
      "how": "Enable lazy loading by installing Microsoft.EntityFrameworkCore.Proxies and configuring: optionsBuilder.UseLazyLoadingProxies(); then ensure navigation properties are virtual.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "添加 .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。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://learn.microsoft.com/en-us/ef/core/querying/related-data/eager",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.9,
  "resolvable": "true",
  "first_seen": "2023-03-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}