Application Version:
Asp Net Core 1.1
EF: Microsoft.EntityFrameworkCore (1.1.1)
Line:
_dbContext.MyTable1.Include(c => c.MyIntermediateTable).ThenInclude(k => k.Select(x => x.MyTable2)).ToList();
Exception:
The property expression 'k => {from MyIntermediateTable x in k select [x].MyTable2}' is not valid. The expression should represent a property access: 't => t.MyProperty'. For more information on including related data
My Entities
[Table("MyTable1")]
public class MyTable1
{
public MyTable1()
{
MyIntermediateTable = new List<MyIntermediateTable>();
}
[Column("MyPK1")]
public int MyPK1 { get; set; }
public string Name{ get; set; }
public virtual List<MyIntermediateTable> MyIntermediateTable{ get; set; }
}
[Table("MyIntermediateTable")]
public class MyIntermediateTable
{
public int MyPK1 { get; set; }
public int MyPK2 { get; set; }
public virtual MyTable1 MyTable1 { get; set; }
public virtual MyTable2 MyTable2 { get; set; }
}
[Table("MyTable2")]
public class MyTable2
{
public MyTable2()
{
MyIntermediateTable = new List<MyIntermediateTable>();
}
[Column("MyPK2")]
public int MyPK2 { get; set; }
public string Name{ get; set; }
public virtual List<MyIntermediateTable> MyIntermediateTable{ get; set; }
}
Model Builder (Fluent API)
modelBuilder.Entity<MyIntermediateTable>((item) =>
{
item.HasKey(p => new { p.MyPK1, p.MyPK2 });
item.HasOne(u => u.MyTable1).WithMany(u => u.MyIntermediateTable).HasForeignKey(u => u.MyPK1);
item.HasOne(u => u.MyTable2).WithMany(u => u.MyIntermediateTable).HasForeignKey(u => u.MyPK2);
});
modelBuilder.Entity<MyTable1>((item) =>
{
item.HasKey(p => p.MyPK1);
item.HasMany(u => u.MyIntermediateTable).WithOne(u => u.MyTable1).HasForeignKey(u => u.MyPK1);
});
modelBuilder.Entity<MyTable2>((item) =>
{
item.HasKey(p => p.MyPK2);
item.HasMany(u => u.MyIntermediateTable).WithOne(u => u.MyTable2).HasForeignKey(u => u.MyPK2);
});