title: Entityframework Core 2.0 error when related object is null in .Include
body: <p>I am using EF core 2.0 and .Net core2.0 to develop my webapi application.</p>
  
  <p>In the following query, i am getting exception when the case doesnt have a workflow ie nullable workflow. the error i get is 'System.NullReferenceException: Object reference not set to an instance of an object.'</p>
  
  <p>I doubt the exception is on the .ThenInclude() when the c.workflow is null.</p>
  
  <p>Case entity:</p>
  
  <pre><code>public class Case 
  {
      [Key]
      public int CaseId { get; set; }
      public int? WorkflowId { get; set; }
      public Workflow Workflow { get; set; }
  }
  </code></pre>
  
  <p>Query:</p>
  
  <pre><code>var item = await _context.Cases
      .Include(c =&gt; c.CaseDetail)
      .Include(c =&gt; c.Workflow).ThenInclude(wf =&gt; wf.CurrentWorkflowLevel)
      .Include(c =&gt; c.Alerts)
      .FirstOrDefaultAsync(c =&gt; c.CaseId == id);
  </code></pre>
  
  <p>Can anyone help how to achieve this query considering the workflow is null?</p>
  
  <p>Exception:</p>
  
  <pre><code>System.NullReferenceException: Object reference not set to an instance of an object.
     at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.IncludeLoadTreeNodeBase.&lt;_AwaitMany&gt;d__8.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.&lt;_IncludeAsync&gt;d__18`1.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.TaskLiftingExpressionVisitor.&lt;_ExecuteAsync&gt;d__8`1.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.AsyncSelectEnumerable`2.AsyncSelectEnumerator.&lt;MoveNext&gt;d__3.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
     at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator`2.&lt;MoveNextCore&gt;d__7.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
     at System.Linq.AsyncEnumerable.AsyncIterator`1.&lt;MoveNext&gt;d__10.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.&lt;MoveNext&gt;d__5.MoveNext()
  Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Exception","time":"2017-11-16T16:16:29.4502900Z","tags":{"ai.operation.id":"b6360c2b-4dabca2bb91d06ac","ai.application.ver":"1.0.0.0","ai.internal.nodeName":"UK2207793W1","ai.cloud.roleInstance":"UK2207793W1","ai.location.ip":"::1","ai.operation.name":"GET Cases/GetCases","ai.operation.parentId":"|b6360c2b-4dabca2bb91d06ac.","ai.internal.sdkVersion":"aspnet5c:2.1.1"},"data":{"baseType":"ExceptionData","baseData":{"ver":2,"properties":{"DeveloperMode":"true","Exception":"System.NullReferenceException: Object reference not set to an instance of an object.\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.IncludeLoadTreeNodeBase.&lt;_AwaitMany&gt;d__8.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.&lt;_IncludeAsync&gt;d__18`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.TaskLiftingExpressionVisitor.&lt;_ExecuteAsync&gt;d__8`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.AsyncSelectEnumerable`2.AsyncSelectEnumerator.&lt;MoveNext&gt;d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n   at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator`2.&lt;MoveNextCore&gt;d__7.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n   at System.Linq.AsyncEnumerable.AsyncIterator`1.&lt;MoveNext&gt;d__10.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.&lt;MoveNext&gt;d__5.MoveNext()","AspNetCoreEnvironment":"Development","error":"System.NullReferenceException: Object reference not set to an instance of an object.\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.IncludeLoadTreeNodeBase.&lt;_AwaitMany&gt;d__8.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.&lt;_IncludeAsync&gt;d__18`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.TaskLiftingExpressionVisitor.&lt;_ExecuteAsync&gt;d__8`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.AsyncSelectEnumerable`2.AsyncSelectEnumerator.&lt;MoveNext&gt;d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n   at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator`2.&lt;MoveNextCore&gt;d__7.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n   at System.Linq.AsyncEnumerable.AsyncIterator`1.&lt;MoveNext&gt;d__10.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.&lt;MoveNext&gt;d__5.MoveNext()","CategoryName":"Microsoft.EntityFrameworkCore.Query","contextType":"FDA.Operational.Core.Workflow.DBContext.WorkflowDBContext","{OriginalFormat}":"An exception occurred in the database while iterating the results of a query for context type '{contextType}'.{newline}{error}"},"exceptions":[{"id":22294293,"typeName":"System.NullReferenceException","message":"An exception occurred in the database while iterating the results of a query for context type 'FDA.Operational.Core.Workflow.DBContext.WorkflowDBContext'.\r\nSystem.NullReferenceException: Object reference not set to an instance of an object.\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.IncludeLoadTreeNodeBase.&lt;_AwaitMany&gt;d__8.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler.&lt;_IncludeAsync&gt;d__18`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.TaskLiftingExpressionVisitor.&lt;_ExecuteAsync&gt;d__8`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.AsyncSelectEnumerable`2.AsyncSelectEnumerator.&lt;MoveNext&gt;d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n   at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator`2.&lt;MoveNextCore&gt;d__7.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n   at System.Linq.AsyncEnumerable.AsyncIterator`1.&lt;MoveNext&gt;d__10.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.&lt;MoveNext&gt;d__5.MoveNext()","hasFullStack":true,"parsedStack":[{"level":0,"method":"Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler+IncludeLoadTreeNodeBase+&lt;_AwaitMany&gt;d__8.MoveNext","assembly":"Microsoft.EntityFrameworkCore, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60"},{"level":1,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":2,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":3,"method":"Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler+&lt;_IncludeAsync&gt;d__18`1.MoveNext","assembly":"Microsoft.EntityFrameworkCore, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60"},{"level":4,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":5,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":6,"method":"System.Runtime.CompilerServices.TaskAwaiter`1.GetResult","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":7,"method":"Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.TaskLiftingExpressionVisitor+&lt;_ExecuteAsync&gt;d__8`1.MoveNext","assembly":"Microsoft.EntityFrameworkCore, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60"},{"level":8,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":9,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":10,"method":"Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+AsyncSelectEnumerable`2+AsyncSelectEnumerator+&lt;MoveNext&gt;d__3.MoveNext","assembly":"Microsoft.EntityFrameworkCore, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60"},{"level":11,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":12,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":13,"method":"System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter.GetResult","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":14,"method":"System.Linq.AsyncEnumerable+SelectEnumerableAsyncIterator`2+&lt;MoveNextCore&gt;d__7.MoveNext","assembly":"System.Interactive.Async, Version=3.0.3000.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263"},{"level":15,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":16,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":17,"method":"System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter.GetResult","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":18,"method":"System.Linq.AsyncEnumerable+AsyncIterator`1+&lt;MoveNext&gt;d__10.MoveNext","assembly":"System.Interactive.Async, Version=3.0.3000.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263"},{"level":19,"method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":20,"method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":21,"method":"System.Runtime.CompilerServices.TaskAwaiter`1.GetResult","assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"},{"level":22,"method":"Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider+ExceptionInterceptor`1+EnumeratorExceptionInterceptor+&lt;MoveNext&gt;d__5.MoveNext","assembly":"Microsoft.EntityFrameworkCore, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60"}]}],"severityLevel":"Error"}}}
  Exception thrown: 'System.NullReferenceException' in System.Private.CoreLib.dll
  Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executed action FDA.Operational.Core.Workflow.WebAPI.Controllers.CasesController.GetCases (FDA.Operational.Core.Workflow.WebAPI) in 1562.4148ms
  Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Message","time":"2017-11-16T16:16:29.6814886Z","tags":{"ai.operation.id":"b6360c2b-4dabca2bb91d06ac","ai.application.ver":"1.0.0.0","ai.internal.nodeName":"UK2207793W1","ai.cloud.roleInstance":"UK2207793W1","ai.location.ip":"::1","ai.operation.name":"GET Cases/GetCases","ai.operation.parentId":"|b6360c2b-4dabca2bb91d06ac.","ai.internal.sdkVersion":"aspnet5c:2.1.1"},"data":{"baseType":"MessageData","baseData":{"ver":2,"message":"Executed action FDA.Operational.Core.Workflow.WebAPI.Controllers.CasesController.GetCases (FDA.Operational.Core.Workflow.WebAPI) in 1562.4148ms","severityLevel":"Information","properties":{"DeveloperMode":"true","ElapsedMilliseconds":"1562.4148","ActionName":"FDA.Operational.Core.Workflow.WebAPI.Controllers.CasesController.GetCases (FDA.Operational.Core.Workflow.WebAPI)","AspNetCoreEnvironment":"Development","CategoryName":"Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker","{OriginalFormat}":"Executed action {ActionName} in {ElapsedMilliseconds}ms"}}}}
  Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware:Error: An unhandled exception has occurred while executing the request
  </code></pre>
  
  <p>The query works fine if case.workflowid is not null. Exception occurs when it is null and it happens at .ThenInclude() when its previous entity is null</p>
  
  <p>Currently i am trying somethign like below. Not sure it is the correct way:</p>
  
  <pre><code>    var unassignedCases = await _context.Cases
                                .Include(c =&gt; c.CaseDetail)
                                .Include(c =&gt; c.Alerts)
                                .Where(c=&gt;c.Workflow == null)
                                .ToListAsync();
  
      var assignedCases = await _context.Cases
                                .Include(c =&gt; c.CaseDetail)
                                .Include(c =&gt; c.Workflow).ThenInclude(wf =&gt; wf.WorkflowRule)
                                .Include(c =&gt; c.Workflow).ThenInclude(wf =&gt; wf.CurrentWorkflowLevel).ThenInclude(wfl =&gt; wfl.FromRules)
                                .Include(c =&gt; c.Alerts)
                                .Where(c =&gt; c.Workflow != null)
                                .ToListAsync();
  </code></pre>