Entityframework批量删除
生活随笔
收集整理的這篇文章主要介紹了
Entityframework批量删除
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
UI層
1 <a href="#" οnclick="DelData(0);return false;" id="a_del" class="easyui-linkbutton" iconcls="icon-cancel">刪除</a>UI中js:
1 //刪除按鈕事件 2 function DelData(id) { 3 $.messager.confirm('提示', '確認刪除?', function (r) { 4 if (r) { 5 var selected = ""; 6 if (id <= 0) { 7 $($('#tab_list').datagrid('getSelections')).each(function () { 8 selected += this.ID + ","; 9 }); 10 selected = selected.substr(0, selected.length - 1); 11 if (selected == "") { 12 $.messager.alert('提示', '請選擇要刪除的數據!', 'info'); 13 return; 14 } 15 } 16 else { 17 selected = id; 18 } 19 $.post('/DataGrid/GetJson', { "action": "del", "cbx_select": selected }, function (data) { 20 $.messager.alert('提示', data, 'info', function () { $("#tab_list").datagrid("reload"); }); 21 }); 22 } 23 }); 24 }MVC中控制器:
1.action:
1 [HttpPost] 2 public ActionResult GetJson() 3 { 4 UserManagerServiceClient client = new UserManagerServiceClient(); 5 6 string action = string.Empty; 7 if (Request.Form["action"] != "") 8 { 9 action = Request.Form["action"].ToString(); 10 } 11 switch (action) 12 { 13 case "query": //第一次進入頁面時查詢數據 14 string JsonString = QueryEmployee(client); 15 return Content(JsonString.ToString()); 16 case "submit": 17 string UpdateMsg = UpdateEmployInfo(client); 18 return Content(UpdateMsg); 19 case "queryone": 20 string JsonOneEmployee = QueryOneEmployee(client); 21 return Content(JsonOneEmployee); 22 case "del" : 23 string DelMsg = DelEmployees(client); 24 return Content(DelMsg); 25 default: 26 return Content(""); 27 }?
2.DelEmployees()方法:
/// <summary>/// 刪除員工信息/// </summary>/// <param name="client"></param>/// <returns></returns>private string DelEmployees(UserManagerServiceClient client){string msg = "刪除失敗!";string selectedID = Request.Form["cbx_select"] != "" ? Request.Form["cbx_select"] : "";if (selectedID != "" && selectedID !="0"){int delCount = client.DelEmployee(selectedID); // 從服務端返回的刪除員工信息的個數if (delCount > 0){msg = string.Format("本次共刪除了{0}條員工信息!", delCount);}}return msg;}WCF服務端代碼:EF中contains就 好似sql中的in
/// <summary>/// 刪除員工信息/// </summary>/// <param name="id"></param>/// <returns></returns>public int DelEmployee(string id){int msg = 0;try{List<string> strID = id.Split(',').ToList();List<int> arrayID = strID.ConvertAll(e => int.Parse(e)); using (UserManageDB db = new UserManageDB()){using (TransactionScope transaction =new TransactionScope() ){foreach (var employee in db.EmployeInfo.Where(e => arrayID.Contains(e.ID))){db.EmployeInfo.Remove(employee);msg++;} db.SaveChanges();transaction.Complete();}}return msg;}catch (Exception ex){ throw ex;}}?
?
轉載于:https://www.cnblogs.com/lihongchen/p/3640613.html
總結
以上是生活随笔為你收集整理的Entityframework批量删除的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VS2017 CUDA编程学习实例1:C
- 下一篇: 实用crontab命令