第三节:EF
1.删除要进行判空
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public ActionResult DelClassMethod(string gId) { //根据gId查询对应条目 var grade = oc.BllSession.IGradeBLL.Entities.Where(a => a.gId == gId).FirstOrDefault(); var stuInfo = oc.BllSession.IStuInfoBLL.Entities.Where(b => b.gId == gId).FirstOrDefault(); //进行删除 if (stuInfo != null) { oc.BllSession.IStuInfoBLL.DelNo(stuInfo); } if (grade != null) { oc.BllSession.IGradeBLL.DelNo(grade); } int result = oc.BllSession.IGradeBLL.SaveChange(); if (result > 0) { return Content("ok"); } else { return Content("error"); } }
2.批量删除
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public ActionResult DelStuMethod(string idsStr) 2 { 3 //进行字符串拆分 4 string[] str = idsStr.Split(new char[] { ',' }); 5 int[] str_int = Array.ConvertAll(str, int.Parse); 6 List<int> stuIdList = str_int.ToList(); 7 var stuList = oc.BllSession.IStuInfoBLL.Entities.Where(a => stuIdList.Contains(a.sId)).ToList(); 8 //删除操作 9 foreach (var item in stuList) 10 { 11 oc.BllSession.IStuInfoBLL.DelNo(item); 12 } 13 int result = oc.BllSession.IGradeBLL.SaveChange(); 14 15 //int result = oc.BllSession.IStuInfoBLL.DelBy(a => stuIdList.Contains(a.sId)); 16 if (result > 0) 17 { 18 return Content("ok"); 19 } 20 else 21 { 22 return Content("error"); 23 } 24 }
作者:chenze 出处:https://www.cnblogs.com/chenze-Index/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |