All*_*olo 25 c# entity-framework entity-framework-core
我在执行查询时要包含三个模型.
这是场景.
public class Sale
{
public int Id { get; set; }
public List<SaleNote> SaleNotes { get; set; }
}
public class SaleNote
{
public int Id { get; set; }
public User User { get; set; }
}
public class User
{
public int Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我可以像这样急切加载SaleNotes ......
_dbContext.Sale.Include(s => s.SaleNotes);
Run Code Online (Sandbox Code Playgroud)
但是,尝试使用ThenInclude从SaleNote急切加载User模型是一项挑战,因为它是一个集合.我找不到任何关于如何加载此方案的示例.有人可以提供下面的代码ThenInclude来加载集合中每个项目的用户.
_dbContext.Sale.Include(s => s.SaleNotes).ThenInclude(...);
Run Code Online (Sandbox Code Playgroud)
oct*_*ccl 41
这与SaleNotes集合导航属性无关.对于引用和集合,它应该是相同的:
_dbContext.Sale.Include(s => s.SaleNotes).ThenInclude(sn=>sn.User);
Run Code Online (Sandbox Code Playgroud)
但据我所知,EF7还支持使用Select扩展方法的旧的多级Include语法:
_dbContext.Sale.Include(s => s.SaleNotes.Select(sn=>sn.User));
Run Code Online (Sandbox Code Playgroud)
作为参考,最新版本的EF Core(1.1.0)也支持此方案的显式加载。像这样
using (var _dbContext = new DbContext())
{
var sale = _dbContext.Sale
.Single(s => s.Id == 1);
_dbContext.Entry(sale)
.Collection(n => n.SalesNotes)
.Load();
_dbContext.Entry(sale)
.Reference(u => u.User)
.Load();
}
Run Code Online (Sandbox Code Playgroud)