public Student Enroll(Guid id, Guid courseId) { Student student = _studentRepository.FindStudentWithCoursesAndAssessmentById(id); StudentCourse alreadyEnrolledCourse = student.StudentCourses .FirstOrDefault(stdcou => stdcou.CourseId.Equals(courseId)); if (alreadyEnrolledCourse != null && !alreadyEnrolledCourse.IsEnrolled) { List <StudentCourse> studentCourses = student.StudentCourses.ToList(); studentCourses.Remove(alreadyEnrolledCourse); alreadyEnrolledCourse.IsEnrolled = true; studentCourses.Add(alreadyEnrolledCourse); student.StudentCourses = studentCourses; return(_studentRepository.Update(student)); } StudentCourse studentCourse = new StudentCourse { StudentId = id, CourseId = courseId, IsEnrolled = true }; student.StudentCourses.Add(studentCourse); List <Assessment> courseAssessments = _assessmentRepository.FindAllCourseAssessment(courseId); foreach (var assessment in courseAssessments) { StudentAssessment studentAssessment = new StudentAssessment { StudentId = id, AssessmentId = assessment.Id }; student.StudentAssessments.Add(studentAssessment); } _studentRepository.Update(student); return(student); }