public async Task <ActionResult> Register(int id) { Course registerCourse = null; Student registerStudent = null; using (_courseRepository = new CoursesRepository()) { registerCourse = await _courseRepository.GetAsync(id); } using (_studentRepository = new StudentRepository()) { var allStudents = await _studentRepository.GetListWithUserAsync(); registerStudent = allStudents.FirstOrDefault(f => f.User.UserName == HttpContext.User.Identity.Name); } using (_enrollmentRepository = new EnrollmentRepository()) { Enrollment newEnrollment = new Enrollment() { CourseID = registerCourse.CourseID, StudentID = registerStudent.StudentID }; var allEnrollments = await _enrollmentRepository.GetListAsync(); if (!allEnrollments.Contains(newEnrollment)) { await _enrollmentRepository.AddAsync(newEnrollment); } else { return(new HttpStatusCodeResult(404)); } } return(RedirectToAction("SpecificCourse/" + id)); }