public bool NewCourse(string Name, int IsElective, int Credit) { try { using (var context = new course_selectionContext()) { var data = new CourseDb(); int newid = context.CourseDb.ToList().Count() == 0 ? 1 : context.CourseDb.ToList().Max(x => x.Code) + 1; data.Code = newid; data.Name = Name; data.IsElective = IsElective; data.Credit = Credit; context.CourseDb.Add(data); context.SaveChanges(); Console.WriteLine("新课程的课程编号为:" + newid); } } catch (Exception e) { Console.WriteLine(e.Message); return(false); } return(true); }
public bool Delete(int id) { try { using (var context = new course_selectionContext()) { var data = context.StudentDb.Where(x => x.Id == id).FirstOrDefault(); context.StudentDb.Remove(data); context.SaveChanges(); } } catch (Exception e) { Console.WriteLine(e.Message); return(false); } return(true); }
public bool Change(int id, string Name, string Class) { try { using (var context = new course_selectionContext()) { var data = context.StudentDb.First(x => x.Id == id); data.Name = Name; data.Class = Class; context.SaveChanges(); } } catch (Exception e) { Console.WriteLine(e.Message); return(false); } return(true); }
public bool Change(int id, string Name, int IsElective, int Credit) { try { using (var context = new course_selectionContext()) { var data = context.CourseDb.First(x => x.Code == id); data.Credit = Credit; data.IsElective = IsElective; data.Name = Name; context.SaveChanges(); } } catch (Exception e) { Console.WriteLine(e.Message); return(false); } return(true); }
public bool Delete(int id) { try { using (var context = new course_selectionContext()) { var data = context.CourseDb.Where(x => x.Code == id).FirstOrDefault(); if (data.IsElective == 1 && data.StudentCourseMaping != null) { throw new Exception("已有学生选修该选修课程,无法删除"); } context.CourseDb.Remove(data); context.SaveChanges(); } } catch (Exception e) { Console.WriteLine(e.Message); return(false); } return(true); }
public bool Delete(int CourseId, int StudentId) { try { using (var context = new course_selectionContext()) { var data = context.StudentCourseMaping.Where(x => x.CourseId == CourseId && x.StudentId == StudentId).FirstOrDefault(); if (data == null) { throw new Exception("信息有误"); } context.StudentCourseMaping.Remove(data); context.SaveChanges(); } } catch (Exception e) { Console.WriteLine(e.Message); return(false); } return(true); }
public bool select(int CourseId, int StudentId) { try { using (var context = new course_selectionContext()) { if (context.StudentDb.Where(x => x.Id == StudentId).ToList().Count() == 0) { throw new Exception("学生不存在"); } if (context.CourseDb.Where(x => x.Code == CourseId).ToList().Count() == 0) { throw new Exception("课程不存在"); } if (context.StudentCourseMaping.Where(x => x.CourseId == CourseId && x.StudentId == StudentId).Count() != 0) { throw new Exception("不允许重复选课"); } var data = new StudentCourseMaping(); int id = context.StudentCourseMaping.ToList().Count() == 0 ? 1 : context.StudentCourseMaping.ToList().Max(x => x.Id) + 1; data.Id = id; data.StudentId = StudentId; data.CourseId = CourseId; context.StudentCourseMaping.Add(data); context.SaveChanges(); } } catch (Exception e) { Console.WriteLine(e.Message); return(false); } return(true); }
public bool NewStudent(string Name, string Class) { try { using (var context = new course_selectionContext()) { var data = new StudentDb(); int newid = context.StudentDb.ToList().Count() == 0 ? 1 : context.StudentDb.ToList().Max(x => x.Id) + 1; data.Id = newid; data.Name = Name; data.Class = Class; context.StudentDb.Add(data); context.SaveChanges(); Console.WriteLine("添加学生的学生编号为:" + newid); } } catch (Exception e) { Console.WriteLine(e.Message); return(false); } return(true); }