public static void EnrollStudent(StudentCourse studentCourse) { if (studentCourse != null) { using (var context = new EFCoreRefContext()) { Student student = context.Student.FirstOrDefault(s => s.ID == studentCourse.StudentID); Course course = context.Course.FirstOrDefault(c => c.ID == studentCourse.CourseID); if (student != null && course != null) { //The student and course exist StudentCourse CurrentStudentCourses = context.StudentCourse .Where(sc => sc.StudentID == studentCourse.StudentID) .Where(sc => sc.CourseID == studentCourse.CourseID) .FirstOrDefault(); if (CurrentStudentCourses == null) { //The student isn't currently enrolled so we are good to add them to the course context.StudentCourse.Add(studentCourse); context.SaveChanges(); } } } } }
public static void DeleteAll() { using (var context = new EFCoreRefContext()) { context.Student.Clear(); context.SaveChanges(); } }
public static List<Student> List() { using (var context = new EFCoreRefContext()) { var students = context.Student .ToList(); return students; } }
public static List<Course> List() { using (var context = new EFCoreRefContext()) { var courses = context.Course .ToList(); return courses; } }
public static List<Course> ListCourses(int studentID) { using (var context = new EFCoreRefContext()) { //Get courses where the studentID is in the StudentCourses join table var StudentCourses = context.Course .Where(s => s.StudentCourses.Any(sc => sc.Student.ID == studentID)) .ToList(); return StudentCourses; } }
public static void AddStudent(Student student) { using (var context = new EFCoreRefContext()) { if (student != null) { context.Student.Add(student); context.SaveChanges(); } } }
public static void AddCourse(Course course) { using (var context = new EFCoreRefContext()) { if (course != null) { context.Course.Add(course); context.SaveChanges(); } } }
public static Student GetStudentByID(int studentID) { using (var context = new EFCoreRefContext()) { var student = context.Student .Include(s => s.StudentCourses).ThenInclude(c => c.Course) .Where(s => s.ID == studentID) .FirstOrDefault(); return student; } }
public static Course GetCourseByID(int courseID) { using (var context = new EFCoreRefContext()) { var course = context.Course .Include(c => c.StudentCourses).ThenInclude(s => s.Student) .Where(c => c.ID == courseID) .FirstOrDefault(); return course; } }