Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core Include On Collection Class Property [duplicate]

I keep getting errors like

: The property expression 'met => {from TrM x in met select [x].Tm}' is not valid. The expression should represent a property access: 't => t.MyProperty'.

I have a class structure of

 public class Tr: BaseModel
{
    public int Id{ get; set; }

    public List<Trm> Mets { get; set; } = new List<Trm>();

    [JsonIgnore]
    public Test TestDef { get; set; }
}



    public class Trm: BaseModel
{

    public Tm tm { get; set; }
}


public class Tm: BaseModel
{

    [JsonIgnore]
    public T TestDef { get; set; }
}

I want to be able to say when loading Tr load all Trm and include Tm when loading.

I have tried the following

 var results = await _dbContext.Tr
                .Include(tr => tr.Mets ).ThenInclude(met => met.Select(x=> x.tm))
                .Include(tr => tr.TestDef)
                .AsNoTracking()
                .ToListAsync();
            return results;

How would I do this?

Thanks

like image 227
Lemex Avatar asked Jan 25 '18 17:01

Lemex


1 Answers

You can't use Select for Include in Ef Core. You should drill down to load related data by using ThenInclude.

   var results = await _dbContext.Tr
    .Include(tr => tr.Mets )
         .ThenInclude(met => met.tm)
    .Include(tr => tr.TestDef)
    .AsNoTracking()
    .ToListAsync();

Here is the offical documentation.

like image 107
lucky Avatar answered Oct 04 '22 03:10

lucky