例子:
public int Add(Student stu)
{
db.Students.Add(stu);//更改状态为“添加”
return db.SaveChanges();//受影响行数
}
二、删除
public int Del(int id)
{
//先通过id查询
var item= db.Students.Find(id);
//删除(更改状态)
db.Students.Remove(item);
//操作数据库
return db.SaveChanges();
}
三、批量删除
public int Del(string id)
{
//获取的id用“,”隔开
var idd = id.Split(‘,‘);
//循环遍历获取选中的id
foreach (var item in idd)
{
//强转int类型
var ids = Convert.ToInt32(item);
var list = mm.Vacancyinfos.Find(ids);
//进行删除
mm.Vacancyinfos.Remove(list);
}
//返回受影响行数
return mm.SaveChanges();
}
四、显示
public List<Vacancyinfo> ShowPage(int? Lid, int? Bid, int pageindex, int pagesize, out int totalcount, out int totalpage) { IQueryable<Vacancyinfo> list = mm.Vacancyinfos.Include("Bumen").Include("Leimu").Include("Chengshi"); if (Lid != null) { list = list.Where(k => k.Lid == Lid); } if (Bid != null) { list = list.Where(k => k.Bid == Bid); } totalcount = list.Count(); totalpage = Convert.ToInt32(Math.Ceiling(totalcount * 1.0 / pagesize)); return list.OrderBy(k => k.VId).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList(); }
五、编辑
public Student FinById(int id)
{
return db.Students.Find(id);//按id查询
}
public int Edit(Student stu)
{
//将实体类的状态改为“修改”状态
db.Entry(stu).State = System.Data.Entity.EntityState.Modified;
//受影响行数
return db.SaveChanges();
}