Per un TestType ho voluto includere sia gli oggetti di navigazione Schoolclass che l'Oggetto.
Potrei fare un:
.Include(t => t.TestType)
.ThenInclude(x => x.Subject)
ma non a:
.Include(t => t.TestType)
.ThenInclude(x => x.Subject)
.ThenInclude(x => x.Schoolclass)
Così ho provato un piccolo trucco e questo ha funzionato:
Ho incluso il TestType 2 volte ...
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();
È questo l'approccio ufficiale o ce n'è uno migliore?
AGGIORNARE
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; }
}
il modo migliore è quello che scrivi prima, con due .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();
Se vedi il risultato della query in SQL Profiler, la query è quella che fai, senza ripetere l'inclusione a TestType (solo 1 join con quella tabella)
Hai un altro modo per farlo, ma preferisco la strada prima!
.Include("TestType.Subject")
.Include("TestType.Schoolclass")