EF核心收录类属性

LmC*_*LmC 6 c# linq entity-framework .net-core ef-core-2.0

我不断收到类似的错误

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

我有一个班级结构

 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; }
}
Run Code Online (Sandbox Code Playgroud)

我想说的是,在加载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;
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

谢谢

luc*_*cky 6

不能使用SelectIncludeEF的核心。您应该使用向下钻取以加载相关数据ThenInclude

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

这是官方文档

  • 我实际上发现了这一点,但我的Rosyln编译器版本中存在一个错误,该错误实际上并未自动完成.ThenInclude的属性,因此请通过:( :( (5认同)