title: EF Core 5 "is invalid inside an 'Include' operation, since it does not represent a property access"
body: <p>I'm trying to use EF Core for a many-to-many relationship. I follow this guide: <a href="https://www.entityframeworktutorial.net/efcore/configure-many-to-many-relationship-in-ef-core.aspx" rel="nofollow noreferrer">https://www.entityframeworktutorial.net/efcore/configure-many-to-many-relationship-in-ef-core.aspx</a> and I have this :</p>
  <p><code>DBPizza</code> model class:</p>
  <pre><code>public class DBPizza
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      [Required]
      public string Name { get; set; }
  
      [Required]
      public float Price { get; set; }
  
      public List&lt;DBPizzaIngredient&gt; PizzaIngredients { get; set; }
  }
  </code></pre>
  <p><code>DBIngredient</code> model class:</p>
  <pre><code>public class DBIngredient
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      [Required]
      public string Name { get; set; }
  
      public List&lt;DBPizzaIngredient&gt; PizzaIngredients { get; set; }
  }
  </code></pre>
  <p>And the <code>DBPizzaIngredient</code> class:</p>
  <pre><code>public class DBPizzaIngredient
  {
      public int PizzaId { get; set; }
      public DBPizza Pizza { get; set; }
  
      public int IngredientId { get; set; }
      public DBIngredient Ingredient {get; set; }
  }
  </code></pre>
  <p>There is my <code>DbContext</code>:</p>
  <pre><code>public class DataContext: DbContext
  {
      public DataContext(DbContextOptions&lt;DataContext&gt; options) : base(options)
      {
      }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;DBPizzaIngredient&gt;().HasKey(pi =&gt; new { pi.PizzaId, pi.IngredientId });
  
          modelBuilder.Entity&lt;DBPizzaIngredient&gt;()
                      .HasOne&lt;DBPizza&gt;(pi =&gt; pi.Pizza)
                      .WithMany(pizza =&gt; pizza.PizzaIngredients)
                      .HasForeignKey(pi =&gt; pi.PizzaId);
  
  
          modelBuilder.Entity&lt;DBPizzaIngredient&gt;()
                      .HasOne&lt;DBIngredient&gt;(pi =&gt; pi.Ingredient)
                      .WithMany(ingredient =&gt; ingredient.PizzaIngredients)
                      .HasForeignKey(pi =&gt; pi.IngredientId);
      }
  
      public DbSet&lt;DBUser&gt; Users { get; set; }
      public DbSet&lt;DBPizza&gt; Pizzas { get; set; }
      public DbSet&lt;DBIngredient&gt; Ingredients { get; set; }
      public DbSet&lt;DBPizzaIngredient&gt; PizzaIngredients { get; set; }
  }
  </code></pre>
  <p>And the code where I have an issue:</p>
  <pre><code>public async Task&lt;List&lt;DBPizza&gt;&gt; Pizzas()
  {
      return await _dataContext.Pizzas
                               .Include(pizza =&gt; pizza.PizzaIngredients)
                               .ThenInclude(pi =&gt; pi.PizzaId)
                               .ToListAsync();
  }
  </code></pre>
  <p>I get this exception:</p>
  <blockquote>
  <p>System.InvalidOperationException: The expression 'pi.PizzaId' is invalid inside an 'Include' operation, since it does not represent a property access: 't =&gt; t.MyProperty'. To target navigations declared on derived types, use casting ('t =&gt; ((Derived)t).MyProperty') or the 'as' operator ('t =&gt; (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see <a href="http://go.microsoft.com/fwlink/?LinkID=746393" rel="nofollow noreferrer">http://go.microsoft.com/fwlink/?LinkID=746393</a>.</p>
  </blockquote>
  <p>I read this topic: <a href="https://stackoverflow.com/questions/66229722/invalid-inside-an-include-operation-since-it-does-not-represent-a-property-ac">Invalid inside an &#39;Include&#39; operation, since it does not represent a property access: &#39;t =&gt; t.MyProperty&#39;</a>, but it didn't resolved my issue</p>