日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Entityframework批量删除

發布時間:2023/12/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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批量删除的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。