EF Core 包含在集合中 Class 属性

EF Core Include On Collection Class Property

我不断收到类似

的错误

: 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'.

我有一个class结构

 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; }
}

我希望能够在加载 Tr 时说加载所有 Trm 并在加载时包含 Tm。

我试过以下方法

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

我该怎么做?

谢谢

您不能在 Ef Core 中将 Select 用于 Include。您应该使用 ThenInclude.

向下钻取以加载相关数据
   var results = await _dbContext.Tr
    .Include(tr => tr.Mets )
         .ThenInclude(met => met.tm)
    .Include(tr => tr.TestDef)
    .AsNoTracking()
    .ToListAsync();

这里是官方documentation