//add manytomany to db public static CourseTeacher BindTeacherToCourse(int teacherId, int courseId) { CoursesEntities db = new CoursesEntities(); if (db.CourseTeachers.Where(p => p.TeacherId == teacherId && p.CourseId == courseId).Count() != 0) { return(null); } CourseTeacher newCourseTeacher = new CourseTeacher(); newCourseTeacher.CourseId = courseId; newCourseTeacher.TeacherId = teacherId; db.CourseTeachers.Add(newCourseTeacher); try { db.SaveChanges(); db.Dispose(); } catch (System.Data.Entity.Validation.DbEntityValidationException) { db.Dispose(); return(null); } return(newCourseTeacher); }
//delete manytomany from db public static bool DelBindTeacherToCourse(int id) { CoursesEntities db = new CoursesEntities(); CourseTeacher courseTeacher = db.CourseTeachers.Find(id); if (courseTeacher != null) { db.CourseTeachers.Remove(courseTeacher); try { db.SaveChanges(); db.Dispose(); return(true); } catch (Exception) { db.Dispose(); return(false); } } return(false); }