问题描述
对于TestType,我想同时包含导航道具Schoolclass和Subject。
For a TestType I wanted to include both navigation props Schoolclass and Subject.
我可以这样做:
.Include(t => t.TestType)
.ThenInclude(x => x.Subject)
但不是a:
.Include(t => t.TestType)
.ThenInclude(x => x.Subject)
.ThenInclude(x => x.Schoolclass)
因此,我尝试了一些技巧,并成功了:
Thus I tried a little trick and that worked:
我将TestType包含了2次...
I included the TestType 2 times...
var test = await context.Tests.Where(t => t.SchoolyearId == schoolyearId)
.Include(t => t.TestType)
.ThenInclude(x => x.Subject)
.Include(t => t.TestType)
.ThenInclude(x => x.Schoolclass)
.AsNoTracking()
.ToListAsync();
是官方方法还是更好的方法?
Is that the official approach or is there a better one?
更新
public class TestType
{
public TestType()
{
Tests = new HashSet<Test>();
}
public int Id { get; set; }
public string Name { get; set; }
public int Weight { get; set; }
public ISet<Test> Tests { get; set; }
public Schoolyear Schoolyear { get; set; }
public Schoolclass Schoolclass { get; set; }
public Subject Subject { get; set; }
public int SchoolyearId { get; set; }
}
推荐答案
最好的方法是您之前写过,带有两个.Include(t => t.TestType)
the best way is that you write before, With two .Include(t => t.TestType)
var test = await context.Tests.Where(t => t.SchoolyearId == schoolyearId)
.Include(t => t.TestType)
.ThenInclude(x => x.Subject)
.Include(t => t.TestType)
.ThenInclude(x => x.Schoolclass)
.AsNoTracking()
.ToListAsync();
如果在SQL事件探查器中看到查询结果,则该查询是您假装的,无需重复包含到TestType(只有1个连接到该表)
If you see the query result in SQL Profiler, the query is that you pretend, without repeating the include to TestType (only 1 join with that table)
您还有另一种方法,但是我更喜欢以前的方法!
You have another way to do it, but i prefer the before way!
.Include("TestType.Subject")
.Include("TestType.Schoolclass")
这篇关于一个包含后如何执行多个ThenInclude导航道具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!