Пример #1
0
        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(ca => ca.Course).ThenInclude(c => c.Department)
                                         .OrderBy(i => i.LastName)
                                         .ToListAsync();

            if (id != null)
            {
                InstructorID = id.Value;
                var selectedInstructor = InstructorData.Instructors.Single(i => i.ID == InstructorID);
                InstructorData.Courses = selectedInstructor.CourseAssignments.Select(ca => ca.Course);
            }

            if (courseID != null)
            {
                CourseID = courseID.Value;
                var selectedCourse = InstructorData.Courses.Where(c => c.CourseID == courseID).Single();

                await _context.Entry(selectedCourse).Collection(c => c.Enrollments).LoadAsync();

                foreach (var enrollment in selectedCourse.Enrollments)
                {
                    await _context.Entry(enrollment).Reference(e => e.Student).LoadAsync();
                }
                InstructorData.Enrollments = selectedCourse.Enrollments;
            }
        }
Пример #2
0
        public async Task OnGetAsync(int?id, int?courseID)
        {
            //Безотложная загрузка свойств навигации

            /* методы Include и ThenInclude повторяются для CourseAssignments и Course.
             * Это необходимо для того, чтобы задать безотложную загрузку для двух свойств навигации сущности Course.
             */
            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() -- для подклчения явной занрузки трекинг должен бытьвключен
                                         .OrderBy(i => i.LastName)
                                         .ToListAsync();

            if (id != null)
            {
                /* Из свойства навигации CourseAssignments этого преподавателя
                 * загружается свойство модели представления Courses вместе с сущностями Course.
                 */
                InstructorID = id.Value;
                Instructor instructor = InstructorData.Instructors
                                        .Where(i => i.ID == id.Value).Single();
                InstructorData.Courses = instructor.CourseAssignments.Select(s => s.Course);
            }

            if (courseID != null)
            {
                CourseID = courseID.Value;
                var selectedCourse = InstructorData.Courses
                                     .Where(x => x.CourseID == courseID).Single();
                //---------------- Явная загрузка курсов -------------------------
                await _context.Entry(selectedCourse).Collection(x => x.Enrollments).LoadAsync();

                foreach (Enrollment enrollment in selectedCourse.Enrollments)
                {
                    await _context.Entry(enrollment).Reference(x => x.Student).LoadAsync();
                }
                //---------------- end: Явная загрузка курсов --------------------

                InstructorData.Enrollments = selectedCourse.Enrollments;
            }
        }
        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)

                                         // ** Eager Loading
                                         //.Include(i => i.CourseAssignments)
                                         //    .ThenInclude(i => i.Course)
                                         //        .ThenInclude(i => i.Enrollments)
                                         //            .ThenInclude(i => i.Student)
                                         //.AsNoTracking()
                                         .OrderBy(i => i.LastName)
                                         .ToListAsync();

            if (id != null)
            {
                InstructorID = id.Value;
                Instructor instructor = InstructorData.Instructors
                                        .Where(i => i.ID == id.Value).Single();
                InstructorData.Courses = instructor.CourseAssignments.Select(s => s.Course);
            }

            if (courseID != null)
            {
                CourseID = courseID.Value;
                var selectedCourse = InstructorData.Courses
                                     .Where(x => x.CourseID == courseID).Single();
                // ** Explicit loading
                await _context.Entry(selectedCourse).Collection(x => x.Enrollments).LoadAsync();

                foreach (Enrollment enrollment in selectedCourse.Enrollments)
                {
                    await _context.Entry(enrollment).Reference(x => x.Student).LoadAsync();
                }
                InstructorData.Enrollments = selectedCourse.Enrollments;
            }
        }
Пример #4
0
        public async Task OnGetAsync(int?id, int?courseID)
        {
            #region snippet_query
            InstructorData             = new InstructorIndexData();
            InstructorData.Instructors = await _context.Instructors
                                         .Include(i => i.OfficeAssignment)
                                         .Include(i => i.Courses)
                                         .ThenInclude(c => c.Department)
                                         .OrderBy(i => i.LastName)
                                         .ToListAsync();

            #endregion

            #region snippet_id
            if (id != null)
            {
                InstructorID = id.Value;
                Instructor instructor = InstructorData.Instructors
                                        .Where(i => i.ID == id.Value).Single();
                InstructorData.Courses = instructor.Courses;
            }
            #endregion

            #region snippet_enrollment
            if (courseID != null)
            {
                CourseID = courseID.Value;
                var selectedCourse = InstructorData.Courses
                                     .Where(x => x.CourseID == courseID).Single();
                await _context.Entry(selectedCourse)
                .Collection(x => x.Enrollments).LoadAsync();

                foreach (Enrollment enrollment in selectedCourse.Enrollments)
                {
                    await _context.Entry(enrollment).Reference(x => x.Student).LoadAsync();
                }
                InstructorData.Enrollments = selectedCourse.Enrollments;
            }
            #endregion
        }
Пример #5
0
        public async Task OnGetAsync(int?id, int?courseID)
        {
            CoachData         = new CoachIndexData();
            CoachData.Coaches = await _context.Coaches
                                .Include(i => i.OfficeAssignment)
                                .Include(i => i.TeamAssignments)
                                .ThenInclude(i => i.Team)
                                .ThenInclude(i => i.Department)
                                //.Include(i => i.CourseAssignments)
                                //    .ThenInclude(i => i.Course)
                                //        .ThenInclude(i => i.Enrollments)
                                //            .ThenInclude(i => i.Student)
                                //.AsNoTracking()
                                .OrderBy(i => i.LastName)
                                .ToListAsync();

            if (id != null)
            {
                CoachID = id.Value;
                Coach coach = CoachData.Coaches
                              .Where(i => i.ID == id.Value).Single();
                CoachData.Teams = coach.TeamAssignments.Select(s => s.Team);
            }

            if (courseID != null)
            {
                TeamID = courseID.Value;
                var selectedCourse = CoachData.Teams
                                     .Where(x => x.TeamID == courseID).Single();
                await _context.Entry(selectedCourse).Collection(x => x.Contracts).LoadAsync();

                foreach (Contract enrollment in selectedCourse.Contracts)
                {
                    await _context.Entry(enrollment).Reference(x => x.Player).LoadAsync();
                }
                CoachData.Contracts = selectedCourse.Contracts;
            }
        }