title: EF Core - Not loading all child entities
body: <p>I have 3 entities: </p>
  
  <pre><code>public partial class Category
  {
      public int CategoryID { get; set; }
      public string? CategoryName { get; set; }
  
      public ICollection&lt;BookCategory&gt; BookCategory { get; set; }
  }
  
  public partial class BookCategory
  {
      public int CategoryID { get; set; }
      public int BookID { get; set; }
  
      public Category Category { get; set; }
      public Book Book { get; set; }
  }
  
  public partial class Book
  {
      public int BookID { get; set; }
      public string? Title { get; set; }
  }
  </code></pre>
  
  <p>I wish to return an array of Category's, with a child array of BookCategory which has a one to one with a Book.</p>
  
  <p>Using a call like this;</p>
  
  <pre><code>public async Task&lt;List&lt;Category&gt;&gt; test()
  {
      var query = dbContext.Category
                  .Include(p =&gt; p.BookCategory) 
                  .ThenInclude(pc =&gt; pc.Book)
                  .OrderBy(p =&gt; p.CategoryID) as IQueryable&lt;Category&gt;;
  
      var data = await query.ToListAsync();
  
      return data;
  }
  </code></pre>
  
  <p>I have dummy data like so:</p>
  
  <pre><code>insert into kiosk.Category (CategoryID, CategoryName) values (1, 'Horror')
  insert into kiosk.Category (CategoryID, CategoryName) values (2, 'Fantasy')
  
  insert into kiosk.Book (BookID, Title) values (1, 'Space shooty')
  insert into kiosk.Book (BookID, Title) values (2, 'Elf shooty')
  
  insert into kiosk.BookCategory (BookID, CategoryID) values (1, 2)
  insert into kiosk.BookCategory (BookID, CategoryID) values (2, 2)
  </code></pre>
  
  <p>However the response I receive for this request only gives me one record for category 2 rather the expected two.</p>
  
  <pre><code>[
      {
          "categoryID": 1,
          "categoryName": "Horror",
          "bookCategory": []
      },
      {
          "categoryID": 2,
          "categoryName": "Fantasy",
          "bookCategory": [
              {
                  "categoryID": 2,
                  "bookID": 1,
                  "book": {
                      "bookID": 1,
                      "title": "Space shooty"
                  }
              }
          ]
      }
  ]
  </code></pre>
  
  <p>DBContext:</p>
  
  <pre><code>    public class BooksDbContext : DbContext
      {
  
          public DbSet&lt;Book&gt; book { get; set; } 
          public DbSet&lt;Category&gt; Category { get; set; } 
          public DbSet&lt;BookCategory&gt; BookCategory { get; set; } 
  
          public BooksDbContext()
          { }
  
          public BooksDbContext(DbContextOptions options) : base(options)
          { }
  
          public BooksDbContext(string connectionString)
          {
              this.connectionString = connectionString;
          }
  
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              base.OnModelCreating(modelBuilder);
  
              modelBuilder.HasDefaultSchema("dbo");
  
              modelBuilder.ApplyConfiguration(new BookCategoryConfiguration());
              modelBuilder.ApplyConfiguration(new CategoryTestConfiguration());
              modelBuilder.ApplyConfiguration(new BookConfiguration());
  
          }
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
              base.OnConfiguring(optionsBuilder);
              if (!string.IsNullOrWhiteSpace(connectionString))
              {
                  optionsBuilder.UseSqlServer(connectionString);
              }
          }
      }
  </code></pre>
  
  <p>The Three Configurations are as follow</p>
  
  <pre><code>    public class BookCategoryConfiguration : IEntityTypeConfiguration&lt;BookCategory&gt;
      {
          public void Configure(EntityTypeBuilder&lt;BookCategory&gt; builder)
          {
              builder.ToTable("BookCategory");
              builder.HasKey(x =&gt; x.BookID);
              builder.HasKey(x =&gt; x.CategoryID);         
  
              builder.HasOne(a =&gt; a.Category).WithMany(b =&gt; b.BookCategory).HasForeignKey(c =&gt; c.CategoryID); // FK_POSCategoryProduct_POSCategory
          }
      }
  
  
      public class CategoryTestConfiguration : IEntityTypeConfiguration&lt;Category&gt;
      {
          public void Configure(EntityTypeBuilder&lt;Category&gt; builder)
          {
              builder.ToTable("Category");
              builder.HasKey(x =&gt; x.CategoryID);
  
              builder.Property(x =&gt; x.CategoryID).HasColumnName(@"CategoryID").HasColumnType("int").IsRequired();
              builder.Property(x =&gt; x.CategoryName).HasColumnName(@"CategoryName").HasColumnType("nvarchar").HasMaxLength(50);          
  
          }
      }
  
  
      public class BookConfiguration : IEntityTypeConfiguration&lt;Book&gt;
      {
          public void Configure(EntityTypeBuilder&lt;Book&gt; builder)
          {
              builder.ToTable("Book");
              builder.HasKey(x =&gt; x.BookID);
  
              builder.Property(x =&gt; x.BookID).HasColumnName(@"BookID").HasColumnType("int").IsRequired();
              builder.Property(x =&gt; x.Title).HasColumnName(@"Title").HasColumnType("nvarchar").HasMaxLength(50);
          }
      }
  </code></pre>