1批量添加 db.T_Investigator.AddRange(list)
2批量删除 db.T_Investigator.RemoveRange(list)
3批量修改 for 循环修改。
注意: 先查询出来,最后savechange(),写在一个事务中,一次请求一个上下文。
public int SubmitAll(int[] idList, int userId) { int num = 0; using (var pmdb = new ProjectManagementEntities()) { using (var tran = pmdb.Database.BeginTransaction()) { try { var list = pmdb.T_Investigator.Where(d => idList.Contains(d.InvestigatorID) && d.CreateUserID == userId && d.HasSubmitted == false).ToList(); if (list.Count > 0) { foreach (var item in list) { item.HasSubmitted = true; } num = pmdb.SaveChanges(); tran.Commit(); } } catch (Exception ex) { tran.Rollback();//回滚 throw ex; } } } return num; }
网上案例:
EF中的那些批量操作
http://www.cnblogs.com/mcgrady/archive/2015/03/21/4355282.html