以多个关系加载相关数据

时间:2016-07-11 07:41:11

标签: c# entity-framework entity-framework-core

我有两个实体SessionStudent,以及一个联接实体StudentSessions

现在我有一个配置文件屏幕,需要查询所选学生的所有数据。

  

Student.cs

public class Student
{
    public long ID { get; set; }
    public string ChildName { get; set; }
    public DateTime ChildDOB { get; set; }
    public DateTime AdmissionDate { get; set; }
    /* removed for brewity*/

    public string Address { get; set; }

    public Lead Source { get; set; }
    public Session CurrentSession
    {
        get
        {
            return (StudentSessions != null && StudentSessions.Any(x => x.Session.IsCurrentlyRunning))
                ? StudentSessions.First(x => x.Session.IsCurrentlyRunning).Session : null;
        }
    }

    public List<StudentSession> StudentSessions { get; set; }
}
  

Session.cs

public class Session
{
    public long ID { get; set; }
    public DateTime SessionStart { get; set; }
    public DateTime SessionEnd { get; set; }
    public string Class { get; set; }
    public bool IsCurrentlyRunning { get { return SessionEnd == null || SessionEnd > DateTime.UtcNow; } }

    public Teacher AssignedTeacher { get; set; }
    public List<Staff> AssignedStaff { get; set; }
    public List<StudentSession> StudentSessions { get; set; }
}
  

StudentSession.cs

public class StudentSession
{
    public long ID { get; set; }
    public long StudentID { get; set; }
    public long SessionID { get; set; }

    public Student Student { get; set; }
    public Session Session { get; set; }

    public List<SessionTerm> SessionTerms { get; set; }
}
  

StudentController.cs

public class StudentController : Controller
{
    MasterContext db;

    public StudentController(MasterContext dbcontext) { db = dbcontext; }

    public async Task<IActionResult> Index([FromRoute]long ID, [FromQuery]string view, [FromQuery]int page)
    {
        IQueryable<Student> studentqry;
        switch (view)
        {
            case "ListAll":
                studentqry = from s in db.Students select s;
                return View(viewName: view, model: await studentqry.OrderByDescending(x => x.AdmissionDate).Skip(page * 50).Take(50).ToListAsync());
            case "Create":
            case "Profile":
                studentqry = (from s in db.Students where s.ID == ID select s)
                    .Include(x => x.Source).ThenInclude(x=>x.Interactions)
                    .Include(x => x.StudentSessions);
                return View(viewName: view, model: await studentqry.SingleAsync());
            case "Edit":
            case "EditInteraction":
            case "ListInteractions":
            default: return View(viewName: "Default");
        }
    }
}

由于Student中的StudentSessions具有一对多关系,因此我无法使用StudentSessions中的ThenInclude为所有StudentSessions of Student加载相关的Session数据。

我曾尝试阅读文档并在谷歌上搜索,但由于实体框架核心相对较新,目前没有多少帮助。任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:1)

事实证明,Visual Studio Intellisense存在问题,编写导航属性,编译和工作完美。

.Include(x => x.StudentSessions).ThenInclude(x => x.Session)