For a TestType I wanted to include both navigation props Schoolclass and Subject.
I could do a:
.Include(t => t.TestType)
.ThenInclude(x => x.Subject)
but not a:
.Include(t => t.TestType)
.ThenInclude(x => x.Subject)
.ThenInclude(x => x.Schoolclass)
Thus I tried a little trick and that worked:
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?
UPDATE
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; }
}
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();
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")