title: How to query many-to-many releationship in EF Core
body: <p>I'm using .NET Core and EF Core for a web project. I'm struggling how to query a many-to-many releationship. This is what my models look like:</p>
  
  <pre><code>public class Begrip
  {
      public int ID { get; set; }
      public string Name { get; set; } 
      public string Desc { get; set; }
      [Url]
      public string URL { get; set; } 
      public ICollection&lt;BegripCategory&gt; Categories { get; set; } 
  }
  
  public class Category
  {
      public int ID { get; set; }
      public string Name { get; set; } 
      public ICollection&lt;BegripCategory&gt; Begrippen { get; set; }
  }
  
  public class BegripCategory
  {
      public int begripId { get; set; }
      public Begrip begrip { get; set; } 
      public int categoryId { get; set; }
      public Category category { get; set; } 
  }
  </code></pre>
  
  <p>And my Database context: </p>
  
  <pre><code>public class PBBContext : DbContext
  {
      public PBBContext (DbContextOptions&lt;PBBContext&gt; options)
          : base(options)
      {
      }
  
      public DbSet&lt;PBB.Models.Movie&gt; Movie { get; set; }
      public DbSet&lt;PBB.Models.Begrip&gt; Begrip { get; set; } 
      public DbSet&lt;PBB.Models.Category&gt; Category { get; set; } 
      public DbSet&lt;PBB.Models.BegripCategory&gt; BegripCategory { get; set; }
  
      protected override void OnModelCreating(ModelBuilder modelbuilder)
      {
          modelbuilder.Entity&lt;BegripCategory&gt;().HasKey(bc =&gt; new { bc.begripId, bc.categoryId });
  
          modelbuilder.Entity&lt;BegripCategory&gt;().HasOne(b =&gt; b.begrip).WithMany(bg =&gt; bg.Categories).HasForeignKey(bc =&gt; bc.begripId);
          modelbuilder.Entity&lt;BegripCategory&gt;().HasOne(c =&gt; c.category).WithMany(ca =&gt; ca.Begrippen).HasForeignKey(cc =&gt; cc.categoryId);
      }
  }
  </code></pre>
  
  <p>What im trying to do is to return all the "Begrippen" in a JSON result with all the corresponding "Categories", however, I can't figure out how to get the list of "Categories" for them.</p>
  
  <p>Any ideas? Thanks in advance. </p>