/* * Create By 付文欣 * 根据专业号修改专业名称 * 成功添加返回1,失败返回0,异常返回-1 */ public int changeNameById(int id,String newName) { try { MajorDBContext majorDBContext = new MajorDBContext(); Major major = majorDBContext.majors.Where(m => m.id == id).ToList()[0]; major.name = newName; return majorDBContext.SaveChanges(); } catch (Exception e) { //throw e; //LogUtil.writeLogToFile(e); return -1; } }
/* * Create By 付文欣 * 根据专业号删除专业 * 成功删除返回1,失败返回0,异常返回-1 */ public int deleteMajorById(int id) { try { Major major = null; MajorDBContext majorDBContext = new MajorDBContext(); if ((major = majorDBContext.majors.Find(id)) != null) { majorDBContext.majors.Remove(major); return majorDBContext.SaveChanges(); } else return 0; } catch (Exception e) { //throw e; //LogUtil.writeLogToFile(e); return -1; } }
/* * Create By 付文欣 * 添加一个专业 * 成功添加返回1,失败返回0,异常返回-1 */ public int addMajor(string name) { try { MajorDBContext majorDBContext = new MajorDBContext(); if(majorDBContext.majors.Where(m=> m.name == name).Count() > 0) { return 0; } Major major = new Major(); major.name = name; majorDBContext.majors.Add(major); return majorDBContext.SaveChanges(); } catch (Exception e) { //throw e; //LogUtil.writeLogToFile(e); return -1; } }
/* * Create By 付文欣 * 根据专业号删除多个专业 * 成功删除返回大于0的数,失败返回0,异常返回-1 */ public int deleteMajorsByIds(List<String> ids) { try { Major major; MajorDBContext majorDBContext = new MajorDBContext(); foreach (var id in ids) { if ((major = majorDBContext.majors.Find(id)) != null) { majorDBContext.majors.Remove(major); } else return 0; } return majorDBContext.SaveChanges(); } catch (Exception e) { //throw e; //LogUtil.writeLogToFile(e); return -1; } }