public static void DeleteStudentByID(int sid) { using (MyContext context = new MyContext()) { var query = from p in context.Students where p.StudentID == sid select p; var std = query.FirstOrDefault(); context.Students.Remove(std); context.SaveChanges(); } }
public static int SaveStudent(Student stdObj) { using (MyContext context = new MyContext()) { if (stdObj.StudentID > 0) { var std = context.Students.Where(p => p.StudentID == stdObj.StudentID).FirstOrDefault(); std.Name = stdObj.Name; std.Address = stdObj.Address; std.Age = stdObj.Age; } else { context.Students.Add(stdObj); } context.SaveChanges(); } return stdObj.StudentID; }
public bool GreateProduct(Product info) { using (MyContext db = new MyContext()) { bool a = false; try { db.Product.Add(info); int b = db.SaveChanges(); if (b == 1) { a = true; } return a; } catch (Exception ex) { return a; } } }
public bool UpdateProduct(Product info) { using (MyContext db = new MyContext()) { bool a = false; try { db.Entry(info).State = EntityState.Modified; // int b = db.SaveChanges(); if (b == 1) { a = true; } return a; } catch (Exception ex) { return a; } } }
public int RemeProduct(string[] Ids) { using (MyContext db = new MyContext()) { int num = 0; try { foreach (string productid in Ids) { int id = Convert.ToInt32(productid); var item = db.Product.Single(g => g.ID == id); db.Product.Remove(item); num = db.SaveChanges(); } return num; } catch (Exception ex) { return num; } } }
/// <summary> /// 添加员工工资记录 /// </summary> /// <param name="t"></param> /// <returns></returns> public int Add(Salary t) { myc.Salaries.Add(t); return(myc.SaveChanges()); }