title: Asynchronous projecting entities with child collection properties using EF Core
body: <p>Using EF Core, I want to asynchronously obtain a list of FooModel which have a collection property of ChildModel. </p>
  
  <pre><code>public class FooModel
  {    
      public Guid Id { get; set; }    
      public string Name { get; set; }
      public List&lt;ChildModel&gt; Childs { get; set; }
  }
  </code></pre>
  
  <p>While the synchronous version returns without issues, the async one will cause the application to freeze. </p>
  
  <pre><code>//Async version.
  public static async Task&lt;List&lt;FooModel&gt;&gt; ListFooModelAsync()
  {
      using (var db = new AppDbContext())
      {
          var foo_items = await db.Foos
              .Include(e =&gt; e.Childs)
              .Select(e =&gt; new FooModel
              {
                  Id = e.Id,            
                  Name = e.Name,
                  Childs = e.Childs.Select(
                      child =&gt; new ChildModel { Id = child.Id, Name = child.Name })
                      .ToList()
              })
              .ToListAsync()
              .ConfigureAwait(false);
          return foo_items;
      }
  }
  </code></pre>
  
  <p>I think the ToList() call on Childs is causing a deadlock somewhere in the pipeline.</p>
  
  <p>If I remove the .ToList() in the Childs construction line it wont freeze and return the list of FooModel,  but its Childs collection will be of type <strong><em>Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.EnumerableAdapter</em></strong>. As soon as I try to use the result in the client the application stop responding, supposedly because EF tries to resolve Childs collection but there is not a DbContext available at that point. </p>
  
  <p>Any thought on how to resolve this?   </p>
  
  <pre><code>//Sync version works fine.
  public static List&lt;FooModel&gt; ListFooModel()
  {
      using (var db = new AppDbContext())
      {
          var foo_items = db.Foos
              .Include(e =&gt; e.Childs)
              .Select(e =&gt; new FooModel
              {
                  Id = e.Id,            
                  Name = e.Name,
                  Childs = e.Childs.Select(
                      child =&gt; new ChildModel { Id = child.Id, Name = child.Name })
                      .ToList()
              })
              .ToList();
          return foo_items;
      }
  }
  </code></pre>