2016-07-11 6 views
-1

私は2つのエンティティSessionStudentと結合エンティティStudentSessionsを持っています。関連するデータを1つの多くの関係でロードする

私は選択された学生のすべてのデータを照会する必要があるプロファイル画面を持っています。

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"); 
     } 
    } 
} 

学生でStudentSessionsは1対多の関係を有するされているので、私は学生の全てStudentSessionsためStudentSessionsでThenIncludeを使用して関連するセッションデータをロードすることはできません。

私はドキュメントを読んでgoogleで検索しようとしましたが、Entity-Framework Coreは比較的新しく、現時点ではあまり役に立ちません。どんな助けも高く評価されます。

+0

多分 '.Include(x => x.StudentSessions).TinInclude(x => x.Session);'?あなたの問題の核心は私には分かりません。 – grek40

+0

@ grek40はありません.ThenInclude(x => x.Session)私はそれに応じて質問を更新しました –

+0

これは実際にはコレクションのために機能するはずですので、あなたの問題は別のものかもしれません。 – grek40

答えて

1

これはVisual Studio Intellisenseの問題で、ナビゲーションプロパティを作成し、コンパイルして完全に機能していました。

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

は、答えを加えることに感謝して2時間を費やした。 – causita

関連する問題