2

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);
        });
Mario Guadanhim
  • 455
  • 8
  • 17
  • @SamiKuhmonen Actually, it does. Examples: http://stackoverflow.com/questions/3356541/entity-framework-linq-query-include-multiple-children-entities – Mario Guadanhim Mar 23 '17 at 20:29
  • @GertArnold it would work if i had one to one relationship – Mario Guadanhim Mar 23 '17 at 20:31
  • @GertArnold the "first" include is a list, so the second include cannot be a "thenInclude", once you try to use the lambda expression, it's gonna appers the "lists options". Please, Try to code it and you'll understand better. (using asp net core, otherwise, yes it's possible) – Mario Guadanhim Mar 24 '17 at 00:28
  • 1
    If you are referring that the Intellisense will only show you the list properties, then it's a bug which happens when there are two overloads which match the signature but one for T other for List of T. Then just type `.ThenInclude(m => m.MyProperty)` even if the intellisense won't show you `MyProperty`. Once you close the method the error will disappear. The Intellisense is confused in this situation and chooses the wrong one – Tseng Mar 24 '17 at 00:33
  • 2
    I do this all the time. `Include(c => c.MyIntermediateTable).ThenInclude(k => k.MyTable2))`. – Gert Arnold Mar 24 '17 at 08:04
  • @Tseng and GetArnold, You guys are right! I trusted too much on Intellisense. It can be the answer for my question. Thanks a lot. – Mario Guadanhim Mar 24 '17 at 13:48

1 Answers1

8

The Intellisense in Visual Studio will only show you the list properties when doing auto-competition.

It's a bug (or something the Intellisense can't handle at the time of the writing) which happens when there are two overloads which match the signature: one for T other for IEnumerable<T>.

When this case appears, then just type .ThenInclude(m => m.MyProperty) even if the intellisense won't show you MyProperty, ignoring any errors the IDE shows you.

Once you close the method brackets, the error will disappear.

Extra information

This bug is tracked by this GitHub Issue Completion missing members of lambda parameter in fault tolerance case on the dotnet roslyn GitHub repository.

Community
  • 1
  • 1
Tseng
  • 52,202
  • 10
  • 166
  • 183