C# 获取根实体及其连接表,并包括其主表

C# 获取根实体及其连接表,并包括其主表,c#,entity-framework,entity-framework-core,C#,Entity Framework,Entity Framework Core,我有一个测试实体: public class Test { public Test() { PupilsTests = new HashSet<PupilTest>(); TestTypeTests = new HashSet<TestTypeTest>(); SchoolclassTests = new HashSet<SchoolclassTest>(); SubjectTes

我有一个测试实体:

public class Test
{
    public Test()
    {
        PupilsTests = new HashSet<PupilTest>();
        TestTypeTests = new HashSet<TestTypeTest>();
        SchoolclassTests = new HashSet<SchoolclassTest>();
        SubjectTests = new HashSet<SubjectTest>();
    }

    public int Id { get; set; }
    public DateTime Date { get; set; }
    public int Number { get; set; }

    public ISet<PupilTest> PupilsTests { get; set; }
    public ISet<TestTypeTest> TestTypeTests { get; set; }
    public ISet<SchoolclassTest> SchoolclassTests { get; set; }
    public ISet<SubjectTest> SubjectTests { get; set; }

    public GradingKey ScoreGradeKey { get; set; }
    public Schoolyear Schoolyear { get; set; }
    public int SchoolyearId { get; set; }

}
公共类测试
{
公开考试()
{
PupilsTests=newhashset();
TestTypeTests=新哈希集();
SchoolclassTests=新HashSet();
SubjectTests=newhashset();
}
公共int Id{get;set;}
公共日期时间日期{get;set;}
公共整数{get;set;}
公共ISet PupilsTests{get;set;}
公共ISet TestTypeTests{get;set;}
公共ISet学校课堂测试{get;set;}
公共ISet主题测试{get;set;}
public GradingKey ScoreGradeKey{get;set;}
公立学年{get;set;}
public int SchoolyearId{get;set;}
}
我需要获取按schoolyearId筛选的所有测试实体,包括连接表SchoolclassTests、SubjectTests、TestTypeTests。

但是对于这些连接表,我还必须包括它们的主要表学校班级、科目、测试类型

这就是我所尝试的:

public async Task<IEnumerable<Test>> GetTestsAsync(int schoolyearId)
{
return await context.Tests.Where(t => t.SchoolyearId == schoolyearId)
                          .Include(t => t.SchoolclassTests)
                          .Include(t => t.SubjectTests)
                          .Include(t => t.TestTypeTests)
                          // How to include all 3 Principal tables? ThenInclude does not workk
                          // over all the 3...
                          .ToListAsync();
}
公共异步任务GetTestsAsync(int schoolyearId) { 返回wait context.Tests.Where(t=>t.SchoolyearId==SchoolyearId) .包括(t=>t.SchoolclassTests) .包括(t=>t.SubjectTests) .Include(t=>t.TestTypeTests) //如何包含所有3个主表?然后包含不起作用 //在所有的3。。。 .ToListAsync(); } 无论我尝试使用什么组合。Include或eninclude,我都不会在一个查询中获得所有3个带有连接表的主表


如何才能做到这一点?

使用
Select
方法,可以包括联接表的主表:

public async Task<IEnumerable<Test>> GetTestsAsync(int schoolyearId)
{
    return await context.Tests.Where(t => t.SchoolyearId == schoolyearId)
            .Include(t => t.SchoolclassTests.Select(s => s.Schoolclass))
            .Include(t => t.SubjectTests.Select(s => s.Subject))
            .Include(t => t.TestTypeTests.Select(t => t.TestType))                
            .ToListAsync();
}
公共异步任务GetTestsAsync(int schoolyearId) { 返回wait context.Tests.Where(t=>t.SchoolyearId==SchoolyearId) .Include(t=>t.SchoolclassTests.Select(s=>s.Schoolclass)) .Include(t=>t.SubjectTests.Select(s=>s.Subject)) .Include(t=>t.TestTypeTests.Select(t=>t.TestType)) .ToListAsync(); }
使用
选择
方法,可以包括联接表的主要表:

public async Task<IEnumerable<Test>> GetTestsAsync(int schoolyearId)
{
    return await context.Tests.Where(t => t.SchoolyearId == schoolyearId)
            .Include(t => t.SchoolclassTests.Select(s => s.Schoolclass))
            .Include(t => t.SubjectTests.Select(s => s.Subject))
            .Include(t => t.TestTypeTests.Select(t => t.TestType))                
            .ToListAsync();
}
公共异步任务GetTestsAsync(int schoolyearId) { 返回wait context.Tests.Where(t=>t.SchoolyearId==SchoolyearId) .Include(t=>t.SchoolclassTests.Select(s=>s.Schoolclass)) .Include(t=>t.SubjectTests.Select(s=>s.Subject)) .Include(t=>t.TestTypeTests.Select(t=>t.TestType)) .ToListAsync(); }
有趣。。。我是在ThenClude上做的,没有想到在Include上使用它。。。一种疾病;-)<代码>选择替换为
然后将
包含在EF Core中。@MohseneMailpour确实这样做了,但它不起作用/无法编译,请将代码示例粘贴到解决方案中:-)有趣。。。我是在ThenClude上做的,没有想到在Include上使用它。。。一种疾病;-)<代码>选择替换为
,然后将
包含在EF Core中。@MohseneMailpour确实这样做了,但它不起作用/无法编译,请将代码示例粘贴到解决方案中:-)