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)
我该怎么做?
谢谢
不能使用Select为IncludeEF的核心。您应该使用向下钻取以加载相关数据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)
这是官方文档。
| 归档时间: |
|
| 查看次数: |
1291 次 |
| 最近记录: |