title: EF Core Include()\ThenInlude() add only one entry to each collection item
body: <p>Building ASP.NET Core API that expose data from existing db.
  All required entities based on data from few tables, so created a view for each.
  API output - JSON with entities hirerachy, created using includes between DbSets.</p>
  <p>There is parent entity <strong>Projects</strong>, which includes child <strong>Components</strong>, which includes <strong>Users</strong>, and some more, but they are not part of the porblem. Simplified DTOs:</p>
  <pre><code>    public class RrojectDTO
      {
          public int projectId { get; set; }
          public string componentCode { get; set; }
          public virtual ICollection&lt;ComponentDTO&gt; componentsArray { get; set; }
      }
      
      public class ComponentDTO
      {
           public Guid componentId { get; set; }
           public virtual RrojectDTO Project { get; set; }
           public string componentCode { get; set; }
           public virtual ICollection&lt;UserDTO&gt; usersArray { get; set; }
           ...
      }
  
      public class UserDTO
      {
           public int userId { get; set; }
           public virtual ComponentDTO Component { get; set; }
           public Guid componentId { get; set; }
           ...
      }
  </code></pre>
  <p>Here is how DBContext class looks like:</p>
  <pre><code>public MyDbContext(DbContextOptions options)
     : base(options)
      {
          ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTrackingWithIdentityResolution;            
      }
  
      public virtual DbSet&lt;ProjectDTO&gt; Projects{ get; set; }
      public virtual DbSet&lt;ComponentDTO&gt; Components { get; set; }
      public virtual DbSet&lt;UserDTO&gt; Users { get; set; }
      ....
  
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
  
          modelBuilder.Entity&lt;ProjectDTO&gt;(entity =&gt;
          {
              entity.HasKey(f =&gt; f.componentCode);
              entity.ToView(&quot;ProjectsArrayView&quot;, &quot;LAB&quot;);
              entity.HasMany(p =&gt; p.componentsArray).WithOne(t =&gt; t.Project)
              .HasForeignKey(t =&gt; t.componentCode);
  
          });
  
          modelBuilder.Entity&lt;ComponentDTO&gt;(entity =&gt;
          {
              entity.HasKey(f =&gt; f.componentId);
              entity.ToView(&quot;ComponentArrayView&quot;, &quot;LAB&quot;);
              entity.HasMany(p =&gt; p.usersArray).WithOne(t =&gt; t.Component)
              .HasForeignKey(t =&gt; t.componentId);
              ....
          });
  
          modelBuilder.Entity&lt;UserDTO&gt;(entity =&gt;
          {
              entity.HasKey(f =&gt; f.userId);
              entity.ToView(&quot;UsersArrayView&quot;, &quot;LAB&quot;);
              ...
          });
  </code></pre>
  <p>I'm connection this entities with next code:</p>
  <pre><code>`var projects = context.Projects.Where(p =&gt; p.projectId == Guid.Parse(&quot;9C373F8F-CAB8-4914-83D9-4864B2E3E7B3&quot;)).Include(co =&gt; co.componentsArray).ThenInclude(j =&gt; j.usersArray).ToList();`
  </code></pre>
  <p>Originally there is more includes to project, but other seems to be working fine. While one on the example does not work as expected, for example: there is one project, that have 3 components, same 2 users(User1 and User2) are working on each component, If you query each dbSet by key parameters, you should be able to see correspondent data. <strong>project</strong> output: 1 project -&gt; with 3 components -&gt; <strong>each component contains only 1 user instead of two.</strong></p>
  <p>My question is: <strong>Why .ThenInlcude() is adding only User1 per component, but ignoring User2 ? what am i missing here ?</strong></p>
  <p>If I query only components DbSet and Include(x=&gt; x.usersArray) - result still the same, User1 per component. Also, if I'm adding User3, User4 - it is still User1 that populated.</p>
  <p>When i found query via SQL profiler, it did contained both users (can't share query due to policies).
  Went over like 20 articles, but I think I'm missing something basic here, please let me know if i can add something to make this qeustion better.</p>