public async Task OnGetAsync(int?id, int?courseID) { Instructor = new InstructorIndexData(); Instructor.Instructors = await _context.Instructors .Include(i => i.OfficeAssignment) .Include(i => i.CourseAssignments) .ThenInclude(i => i.Course) .ThenInclude(i => i.Department) .OrderBy(i => i.LastName) .ToListAsync(); if (id != null) { InstructorID = id.Value; Instructor instructor = Instructor.Instructors.Single(i => i.ID == id.Value); Instructor.Courses = instructor.CourseAssignments.Select(s => s.Course); InstructorName = instructor.FirstMidName + " " + instructor.LastName; } if (courseID != null) { CourseID = courseID.Value; var courses = Instructor.Courses.ToList(); var selectedCourse = courses.Single(x => x.CourseID == courseID); await _context.Entry(selectedCourse).Collection(x => x.Enrollments).LoadAsync(); foreach (Enrollment enrollment in selectedCourse.Enrollments) { await _context.Entry(enrollment).Reference(x => x.Student).LoadAsync(); } Instructor.Enrollments = selectedCourse.Enrollments; CourseName = courses.First(course => course.CourseID == courseID).Title; } }
public async Task OnGetAsync(int?id, int?courseID) { Instructor = new InstructorIndexData(); Instructor.Instructors = await _context.Instructors .Include(i => i.OfficeAssignment) .Include(i => i.CourseAssigments) .ThenInclude(i => i.Course) .ThenInclude(i => i.Department) //.Include(i => i.CourseAssigments) // .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 = Instructor.Instructors.Where(i => i.ID == id.Value).Single(); Instructor.Courses = instructor.CourseAssigments.Select(s => s.Course); } if (courseID != null) { CourseID = courseID.Value; var selectedCourse = Instructor.Courses.Where(x => x.CourseID == courseID).Single(); // this part only includes student info if a course if selected await _context.Entry(selectedCourse).Collection(x => x.Enrollments).LoadAsync(); foreach (Enrollment enrollment in selectedCourse.Enrollments) { await _context.Entry(enrollment).Reference(x => x.Student).LoadAsync(); } Instructor.Enrollments = selectedCourse.Enrollments; } }
public async Task OnGetAsync(int?id, int?courseID) { Instructor = new InstructorIndexData(); Instructor.Instructors = await _context.Instructors .Include(i => i.OfficeAssignment) .Include(i => i.CourseAssignments) .ThenInclude(i => i.Course) .ThenInclude(i => i.Department) // eager loading of enrollments //.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 = Instructor.Instructors.Where( // i => i.ID == id.Value).Single(); Instructor instructor = Instructor.Instructors.Single( i => i.ID == id.Value); Instructor.Courses = instructor.CourseAssignments.Select(s => s.Course); } if (courseID != null) { CourseID = courseID.Value; // with eager loading, uncomment this line. //Instructor.Enrollments = Instructor.Courses.Single( // x => x.CourseID == courseID).Enrollments; // explicit loading, only when requested: var selectedCourse = Instructor.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(); } Instructor.Enrollments = selectedCourse.Enrollments; } }
public async Task OnGetAsync(int?id, int?courseID) { Instructor = new InstructorIndexData(); Instructor.Instructors = await _context.Instructors .Include(i => i.OfficeAssignment) .Include(i => i.CourseAssignments) .ThenInclude(i => i.Course) .ThenInclude(i => i.Department) // // The current code specifies eager loading for Enrollments and Students: // .Include(i => i.CourseAssignments) //.ThenInclude(i => i.Course) // .ThenInclude(i => i.Enrollments) // .ThenInclude(i => i.Student) // //end // .AsNoTracking() .OrderBy(i => i.LastName) .ToListAsync(); if (id != null) { InstructorID = id.Value; Instructor instructor = Instructor.Instructors.Where( i => i.ID == id.Value).Single(); Instructor.Courses = instructor.CourseAssignments.Select(s => s.Course); } if (courseID != null) { CourseID = courseID.Value; //Update the OnGetAsync with the following code: var selectedCourse = Instructor.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(); } Instructor.Enrollments = selectedCourse.Enrollments; //end } }
public async Task OnGetAsync(int?id, int?courseID) { Instructor = new InstructorIndexData(); Instructor.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) { InstructorID = id.Value; //Instructor instructor = Instructor.Instructors.Where( // i => i.ID == id.Value).Single(); //get single value of selected instructor Instructor instructor = Instructor.Instructors.Single(i => i.ID == id.Value); Instructor.Courses = instructor.CourseAssignments.Select(c => c.Course); //Get a list of courses } /*if (courseID != null) * { * CourseID = courseID.Value; * //Instructor.Enrollments = Instructor.Courses.Where(x => x.CourseID == courseID).Single().Enrollments; * Instructor.Enrollments = Instructor.Courses.Single(x => x.CourseID == courseID).Enrollments; * //Get enrollments for the seelected course * }*/ if (courseID != null) { var selectedCourse = Instructor.Courses.Where(x => x.CourseID == courseID).Single(); //Single converts from being a list to a single object await _context.Entry(selectedCourse).Collection(x => x.Enrollments).LoadAsync(); //Loads entities linked to main entity foreach (Enrollment enrollment in selectedCourse.Enrollments) { await _context.Entry(enrollment).Reference(x => x.Student).LoadAsync(); //Loads a single field } Instructor.Enrollments = selectedCourse.Enrollments; } }
public async Task OnGetAsync(int?id, int?courseID) { Instructor = new InstructorIndexData(); Instructor.Instructors = await _context.Instructors .Include(i => i.OfficeAssignment) .Include(i => i.CourseAssignments) .ThenInclude(i => i.Course) .ThenInclude(i => i.Department) // упреждающая загрузка для Enrollments и Students: //.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 = Instructor.Instructors.Where( i => i.ID == id.Value).Single(); Instructor.Courses = instructor.CourseAssignments.Select(s => s.Course); // TODO will knock debt try { Instructor.Enrollments = Instructor.Courses.First().Enrollments; } catch (Exception ignore) {} } if (courseID != null) { CourseID = courseID.Value; var selectedCourse = Instructor.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(); } Instructor.Enrollments = selectedCourse.Enrollments; } }
public async Task OnGetAsync(int?id, int?courseID) { Instructor = new InstructorIndexData(); Instructor.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(ca => ca.Course) .ThenInclude(c => c.Enrollments) .ThenInclude(e => e.Student) .OrderBy(i => i.LastName) .ToListAsync(); if (id != null) { InstructorID = id.Value; Instructor instructor = Instructor.Instructors .Where(i => i.ID == id).Single(); Instructor.Courses = instructor.CourseAssignments.Select(c => c.Course); } if (courseID != null) { CourseID = courseID.Value; var selectedCourse = Instructor.Courses .Single(c => c.CourseID == courseID.Value); await _context.Entry(selectedCourse).Collection(x => x.Enrollments).LoadAsync(); foreach (var enrollment in selectedCourse.Enrollments) { await _context.Entry(enrollment).Reference(x => x.Student).LoadAsync(); } Instructor.Enrollments = selectedCourse.Enrollments; } }
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(await HandleDeletedDepartment()); } _context.Entry(departmentToUpdate).Property("RowVersion").OriginalValue = Department.RowVersion; //The Department.RowVersion is set when the record is read from the database on the initial GET. //if 2RowVersion2 has been updated so it doesn't match Department.RowVersion then Concurrency exception is thrown. //OriginalValue is the database value just before we tried to save 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); Department.RowVersion = (byte[])dbValues.RowVersion; //sets the rowversion to be the correct one from the database ModelState.Remove("Department.RowVersion"); } } InstructorNameSL = new SelectList(_context.Instructors, "ID", "FullName", departmentToUpdate.InstructorID); return(Page()); }
public async Task OnGetAsync(int?id, int?courseID) { Instructor = new InstructorViewModel(); Instructor.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) { InstructorID = id.Value; Instructor instructor = Instructor.Instructors.Single( i => i.ID == id.Value); //You can add condition into Select and remove Where //If you need a single item by condition Instructor.Courses = instructor.CourseAssignments.Select(s => s.Course); } if (courseID != null) { CourseID = courseID.Value; var selectedCourse = Instructor.Courses.Single(x => x.CourseID == courseID); await _context.Entry(selectedCourse).Collection(x => x.Enrollments).LoadAsync(); foreach (Enrollment enrollment in selectedCourse.Enrollments) { await _context.Entry(enrollment).Reference(x => x.Student).LoadAsync(); } Instructor.Enrollments = selectedCourse.Enrollments; } }
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); // null means Department was deleted by another user. if (departmentToUpdate == null) { return(await HandleDeletedDepartment()); } // Update the RowVersion to the value when this entity was // fetched. If the entity has been updated after it was // fetched, RowVersion won't match the DB RowVersion and // a DbUpdateConcurrencyException is thrown. // A second postback will make them match, unless a new // concurrency issue happens. _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 an new concurrency issue happens. Department.RowVersion = (byte[])dbValues.RowVersion; // Must clear the model error for the next postback. ModelState.Remove("Department.RowVersion"); } } InstructorNameSL = new SelectList(_context.Instructors, "ID", "FullName", departmentToUpdate.InstructorID); return(Page()); }
public void UpdateStudent(Student student) { context.Entry(student).State = EntityState.Modified; }
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); // null означает, что отдел был удален другим пользователем. if (departmentToUpdate == null) { return(HandleDeletedDepartment()); } // Обновляем RowVersion до значения, когда этот объект был // извлечен. Если объект был обновлен после того, как // извлечен, RowVersion не будет соответствовать DB RowVersion и // создается исключение DbUpdateConcurrencyException. // Department.RowVersion является значением на момент извлечения // сущности. OriginalValue является значением в базе данных на момент // вызова FirstOrDefaultAsync в этом методе. _context.Entry(departmentToUpdate).Property("RowVersion").OriginalValue = Department.RowVersion; if (await TryUpdateModelAsync( 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); // Сохраняем текущий RowVersion Department.RowVersion = (byte[])dbValues.RowVersion; // Необходимо очистить ошибку модели для следующего постбэка. ModelState.Remove("Department.RowVersion"); } } InstructorNameSL = new SelectList(_context.Instructors, "ID", "FullName", departmentToUpdate.InstructorID); return(Page()); }
public async Task <IActionResult> Edit(int?id, byte[] rowVersion) { if (id == null) { return(NotFound()); } var departmentToUpdate = await _context.Departments .Include(i => i.Administrator) .SingleOrDefaultAsync(m => m.DepartmentID == id); if (departmentToUpdate == null) { Department deleteDepartment = new Department(); await TryUpdateModelAsync(deleteDepartment); ModelState.AddModelError(string.Empty, "Unable to save changes, The department was deleted by another user." ); ViewData["InstructorID"] = new SelectList(_context.Instructors, "ID", "FullName", deleteDepartment.InstructorID); return(View(deleteDepartment)); } _context.Entry(departmentToUpdate).Property("RowVersion").OriginalValue = rowVersion; if (await TryUpdateModelAsync <Department>( departmentToUpdate, "", s => s.Name, s => s.StartDate, s => s.Budget, s => s.InstructorID )) { try { await _context.SaveChangesAsync(); return(RedirectToAction(nameof(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 changes, The department was deleted by another user."); } else { var databaseValues = (Department)databaseEntry.ToObject(); if (databaseValues.Name != clientValues.Name) { ModelState.AddModelError("Name", $"Curent value:{databaseValues.Name}"); } if (databaseValues.Budget != clientValues.Budget) { ModelState.AddModelError("Budget", $"Current value: {databaseValues.Budget}"); } if (databaseValues.StartDate != clientValues.StartDate) { ModelState.AddModelError("StartDate", $"Current Value: {databaseValues.StartDate}"); } if (databaseValues.InstructorID != clientValues.InstructorID) { Instructor databaseInstructor = await _context.Instructors.SingleOrDefaultAsync(i => i.ID == databaseValues.InstructorID); ModelState.AddModelError("InstructorID", $"Current value:{databaseInstructor?.FullName}"); } ModelState.AddModelError(string.Empty, "The record you attempted to edit " + "was modified by another user after you got the orginal value. The" + "edit operation was canceled and the current values in the database" + "have been displayed. If you still want to edit this record, click" + "the Save button agin. Otherwise click the Back to List hyperlink"); departmentToUpdate.RowVersion = (byte [])databaseValues.RowVersion; ModelState.Remove("RowVersion"); } } } ViewData["InstructorID"] = new SelectList(_context.Instructors, "ID", "FullName", departmentToUpdate.InstructorID); return(View(departmentToUpdate)); }