public async Task OnGetAsync(int?id, int?courseID) { InstructorData = new InstructorIndexData(); InstructorData.Instructors = await _context.Instructors .Include(i => i.OfficeAssignment) .Include(i => i.CourseAssignments) .ThenInclude(i => i.Course) .ThenInclude(i => i.Department) //.Include(i=>i.CourseAssignments) // .ThenInclude(i=>i.Course) // .ThenInclude(i=>i.Enrollments) // .ThenInclude(i=>i.Student) //.AsNoTracking() //asnotracking is to be commented out because //navigation properties only be explicitly loaded for the tracked entities //the commented method uses the eager loading //we are now using explicit loading .OrderBy(i => i.LastName) .ToListAsync(); if (id != null) { InstructorID = id.Value; Instructor instructor = InstructorData.Instructors //.Single(i => i.ID == id.Value); .Where(i => i.ID == id.Value).Single(); //single can be used directly in place of line above InstructorData.Courses = instructor.CourseAssignments .Select(s => s.Course); } if (courseID != null) { courseID = courseID.Value; var selectedCourses = InstructorData.Courses .Where(x => x.CourseID == courseID).Single(); //the code below is used fro the explicit loading //i.e. if we rarely want to see enrollments in a course //we only load the enrollment data if it's requested await _context.Entry(selectedCourses).Collection (x => x.Enrollments).LoadAsync(); foreach (Enrollment enrollment in selectedCourses.Enrollments) { await _context.Entry(enrollment).Reference( x => x.Student).LoadAsync(); } InstructorData.Enrollments = selectedCourses.Enrollments; //InstructorData.Enrollments = InstructorData.Courses. // Single(x => x.CourseID == courseID).Enrollments; } }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int id) { if (!ModelState.IsValid) { return(Page()); } var departmentToUpdate = await _context.Departments .Include(i => i.Administrator) .FirstOrDefaultAsync(m => m.DepartmentID == id); if (departmentToUpdate == null) { return(HandleDeletedDepartment()); } _context.Entry(departmentToUpdate) .Property("RowVersion").OriginalValue = Department.RowVersion; if (await TryUpdateModelAsync <Department>( departmentToUpdate, "Department", s => s.Name, s => s.StartDate, s => s.Budget, s => s.InstructorID)) { try { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } catch (DbUpdateConcurrencyException ex) { var exceptionEntry = ex.Entries.Single(); var clientValues = (Department)exceptionEntry.Entity; var databaseEntry = exceptionEntry.GetDatabaseValues(); if (databaseEntry == null) { ModelState.AddModelError(string.Empty, "Unable to Save." + "The department was deleted by another user."); return(Page()); } var dbValues = (Department)databaseEntry.ToObject(); await SetDbErrorMessage(dbValues, clientValues, _context); //Save the current RowVersion so next postback matches //unless a new concurrency issue happens Department.RowVersion = (byte[])dbValues.RowVersion; //Clear the model error for next postback ModelState.Remove("Department.RowVersion"); } } InstructorNameSL = new SelectList(_context.Instructors, "ID", "FullName"); return(Page()); }