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 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(); } } }