/// <summary> /// 添加或修改方向信息 /// </summary> /// <param name="direction"></param> /// <returns></returns> public bool AddDirection(Direction direction, String Name) { Direction dir = dbContext.Directions.Where(x => x.Id == direction.Id).FirstOrDefault(); if (dir == null) { direction.Director = dbContext.Teachers.Where(x => x.Name == Name).FirstOrDefault(); dbContext.Directions.Add(direction); } else { dir.Name = direction.Name; dir.Max = 30; dir.Director = direction.Director; } try { dbContext.SaveChanges(); return(true); } catch { return(false); } }
static void test1() { using (var db = new WdDbContext()) { var s = new Student() { LoginName = "20081101", Name = "yh", Pwd = "123456", Major = "cs", IsTeacher = false }; var t = new Teacher() { LoginName = "JC200803040", Name = "tr", Pwd = "123456", IsTeacher = true }; var d = new Direction() { Name = "dotNET", Director = t }; db.Students.Add(s); db.Teachers.Add(t); db.Directions.Add(d); db.SaveChanges(); } }
/// <summary> /// 将选择顺序保存至数据库 /// </summary> /// <param name="stu">学生</param> /// <param name="directionOrders">方向Id和排序的键值对</param> /// <returns></returns> public bool SelectDirection(string name, Dictionary <int, int> directionOrders) { foreach (var item in directionOrders) { DirectionOrder dd = new DirectionOrder(); dd.Order = item.Value; dd.Student = dbContext.Students.Where(x => x.LoginName == name).FirstOrDefault(); dd.Direction = dbContext.Directions.SingleOrDefault(d => d.Id == item.Key); if (dd.Direction == null) { throw new Exception($"系统错误:方向[{item.Key}]信息未找到!"); } else { dbContext.DirectionOrders.Add(dd); } } try { dbContext.SaveChanges(); return(true); } catch { return(false); } }
/// <summary> /// 添加教师信息 /// </summary> /// <param name="teacher">教师对象</param> /// <returns>是否添加成功</returns> public bool AddTeacher(Teacher teacher) { bool b = false; Teacher t = dbContext.Teachers.Where(x => x.Id == teacher.Id).FirstOrDefault(); if (t == null) { dbContext.Teachers.Add(teacher); int i = dbContext.SaveChanges(); if (i > 0) { b = true; } } return(b); }
/// <summary> /// 添加或者修改学生信息 /// </summary> /// <param name="student">Student 对象</param> /// <returns></returns> public bool AddStudent(Student student) { if (dbContext.Students.Where(x => x.Id == student.Id).FirstOrDefault() == null) { dbContext.Students.Add(student); } else { var dir = dbContext.Students.Where(x => x.Id == student.Id).FirstOrDefault(); dir.IsCompleted = student.IsCompleted; dir.Pwd = student.Pwd; } try { dbContext.SaveChanges(); return(true); } catch { return(false); } }
/// <summary> /// 设置截止时间 /// </summary> /// <param name="theTime">截止时间</param> /// <returns>是否设置成功</returns> public static bool SetDeadline(DateTime theTime) { bool b = false; using (var db = new WdDbContext()) { var config = db.ServerConfigurations.FirstOrDefault(); if (config != null) { config.Deadline = theTime; int i = db.SaveChanges(); if (i > 0) { b = true; } } } return(b); }
/// <summary> /// 事务性保存 /// </summary> public bool Save() { return(_dbContext.SaveChanges() > 0); }