一. 簡介html
該章節,能夠說是一個簡單輕鬆的章節,只要你對Expression表達式樹、EF的基本使用、泛型有所瞭解,那麼本章節實質上就是一個很是簡單的封裝章節,便於咱們快捷開發。sql
PS:在該章節對於EF的上下文怎麼處理,怎麼來的,不作介紹,在後續的框架篇將詳細介紹,下面的EF上下文,將直接使用db代替。數組
若是你對Expression、EF的增刪改查、泛型生疏的話,能夠先閱讀如下章節:框架
(1). Expression表達式目錄樹:http://www.cnblogs.com/yaopengfei/p/7486870.htmlspa
(2). EF的基本增刪改查:http://www.cnblogs.com/yaopengfei/p/7674715.html插件
(3). 泛型的使用:http://www.cnblogs.com/yaopengfei/p/6880629.htmlcode
二. 代碼封裝分享htm
下面的代碼封裝,主要就是圍繞EF的增刪改查進行封裝以及各自對應的擴展,其中包括事務一體的封裝、事務分離的封裝、集成 Z.EntityFramework.Extensions 插件的封裝、以及EF調用SQL語句的封裝。對象
1. EF調用SQL語句:blog
1 /// <summary> 2 /// 執行增長,刪除,修改操做(或調用存儲過程) 3 /// </summary> 4 /// <param name="sql"></param> 5 /// <param name="pars"></param> 6 /// <returns></returns> 7 public int ExecuteSql(string sql, params SqlParameter[] pars) 8 { 9 return db.Database.ExecuteSqlCommand(sql, pars); 10 } 11 12 /// <summary> 13 /// 執行查詢操做 14 /// </summary> 15 /// <typeparam name="T"></typeparam> 16 /// <param name="sql"></param> 17 /// <param name="pars"></param> 18 /// <returns></returns> 19 public List<T> ExecuteQuery<T>(string sql, params SqlParameter[] pars) 20 { 21 return db.Database.SqlQuery<T>(sql, pars).ToList(); 22 }
2. EF增刪改查封裝(事務一體)
(1). 新增
1 public int Add(T model) 2 { 3 DbSet<T> dst = db.Set<T>(); 4 dst.Add(model); 5 return db.SaveChanges(); 6 }
(2). 刪除
1 /// <summary> 2 /// 刪除(適用於先查詢後刪除的單個實體) 3 /// </summary> 4 /// <param name="model">須要刪除的實體</param> 5 /// <returns></returns> 6 public int Del(T model) 7 { 8 db.Set<T>().Attach(model); 9 db.Set<T>().Remove(model); 10 return db.SaveChanges(); 11 } 12 /// <summary> 13 /// 根據條件刪除(支持批量刪除) 14 /// </summary> 15 /// <param name="delWhere">傳入Lambda表達式(生成表達式目錄樹)</param> 16 /// <returns></returns> 17 public int DelBy(Expression<Func<T, bool>> delWhere) 18 { 19 List<T> listDels = db.Set<T>().Where(delWhere).ToList(); 20 listDels.ForEach(d => 21 { 22 db.Set<T>().Attach(d); 23 db.Set<T>().Remove(d); 24 }); 25 return db.SaveChanges(); 26 }
(3). 查詢
1 /// <summary> 2 /// 根據條件查詢 3 /// </summary> 4 /// <param name="whereLambda">查詢條件(lambda表達式的形式生成表達式目錄樹)</param> 5 /// <returns></returns> 6 public List<T> GetListBy(Expression<Func<T, bool>> whereLambda) 7 { 8 return db.Set<T>().Where(whereLambda).ToList(); 9 } 10 /// <summary> 11 /// 根據條件排序和查詢 12 /// </summary> 13 /// <typeparam name="Tkey">排序字段類型</typeparam> 14 /// <param name="whereLambda">查詢條件</param> 15 /// <param name="orderLambda">排序條件</param> 16 /// <param name="isAsc">升序or降序</param> 17 /// <returns></returns> 18 public List<T> GetListBy<Tkey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, Tkey>> orderLambda, bool isAsc = true) 19 { 20 List<T> list = null; 21 if (isAsc) 22 { 23 list = db.Set<T>().Where(whereLambda).OrderBy(orderLambda).ToList(); 24 } 25 else 26 { 27 list = db.Set<T>().Where(whereLambda).OrderByDescending(orderLambda).ToList(); 28 } 29 return list; 30 } 31 /// <summary> 32 /// 分頁查詢 33 /// </summary> 34 /// <typeparam name="Tkey">排序字段類型</typeparam> 35 /// <param name="pageIndex">頁碼</param> 36 /// <param name="pageSize">頁容量</param> 37 /// <param name="whereLambda">查詢條件</param> 38 /// <param name="orderLambda">排序條件</param> 39 /// <param name="isAsc">升序or降序</param> 40 /// <returns></returns> 41 public List<T> GetPageList<Tkey>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, Tkey>> orderLambda, bool isAsc = true) 42 { 43 44 List<T> list = null; 45 if (isAsc) 46 { 47 list = db.Set<T>().Where(whereLambda).OrderBy(orderLambda) 48 .Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); 49 } 50 else 51 { 52 list = db.Set<T>().Where(whereLambda).OrderByDescending(orderLambda) 53 .Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); 54 } 55 return list; 56 } 57 /// <summary> 58 /// 分頁查詢輸出總行數 59 /// </summary> 60 /// <typeparam name="Tkey">排序字段類型</typeparam> 61 /// <param name="pageIndex">頁碼</param> 62 /// <param name="pageSize">頁容量</param> 63 /// <param name="whereLambda">查詢條件</param> 64 /// <param name="orderLambda">排序條件</param> 65 /// <param name="isAsc">升序or降序</param> 66 /// <returns></returns> 67 public List<T> GetPageList<Tkey>(int pageIndex, int pageSize, ref int rowCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, Tkey>> orderLambda, bool isAsc = true) 68 { 69 int count = 0; 70 List<T> list = null; 71 count = db.Set<T>().Where(whereLambda).Count(); 72 if (isAsc) 73 { 74 var iQueryList = db.Set<T>().Where(whereLambda).OrderBy(orderLambda) 75 .Skip((pageIndex - 1) * pageSize).Take(pageSize); 76 77 list = iQueryList.ToList(); 78 } 79 else 80 { 81 var iQueryList = db.Set<T>().Where(whereLambda).OrderByDescending(orderLambda) 82 .Skip((pageIndex - 1) * pageSize).Take(pageSize); 83 list = iQueryList.ToList(); 84 } 85 rowCount = count; 86 return list; 87 }
(4). 修改
1 /// <summary> 2 /// 修改 3 /// </summary> 4 /// <param name="model">修改後的實體</param> 5 /// <returns></returns> 6 public int Modify(T model) 7 { 8 db.Entry(model).State = EntityState.Modified; 9 return db.SaveChanges(); 10 } 11 12 /// <summary> 13 /// 單實體擴展修改(把不須要修改的列用LAMBDA數組表示出來) 14 /// </summary> 15 /// <param name="model">要修改的實體對象</param> 16 /// <param name="ignoreProperties">不需要修改的相關字段</param> 17 /// <returns>受影響的行數</returns> 18 public int Modify(T model, params Expression<Func<T, object>>[] ignoreProperties) 19 { 20 using (DbContext db = new DBContextFactory().GetDbContext()) 21 { 22 db.Set<T>().Attach(model); 23 24 DbEntityEntry entry = db.Entry<T>(model); 25 entry.State = EntityState.Unchanged; 26 27 Type t = typeof(T); 28 List<PropertyInfo> proInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList(); 29 30 Dictionary<string, PropertyInfo> dicPros = new Dictionary<string, PropertyInfo>(); 31 proInfos.ForEach( 32 p => dicPros.Add(p.Name, p) 33 ); 34 35 if (ignoreProperties != null) 36 { 37 foreach (var ignorePropertyExpression in ignoreProperties) 38 { 39 //根據表達式獲得對應的字段信息 40 var ignorePropertyName = new PropertyExpressionParser<T>(ignorePropertyExpression).Name; 41 dicPros.Remove(ignorePropertyName); 42 } 43 } 44 45 foreach (string proName in dicPros.Keys) 46 { 47 entry.Property(proName).IsModified = true; 48 } 49 return db.SaveChanges(); 50 } 51 } 52 53 /// <summary> 54 /// 批量修改(非lambda) 55 /// </summary> 56 /// <param name="model">要修改實體中 修改後的屬性 </param> 57 /// <param name="whereLambda">查詢實體的條件</param> 58 /// <param name="proNames">lambda的形式表示要修改的實體屬性名</param> 59 /// <returns></returns> 60 public int ModifyBy(T model, Expression<Func<T, bool>> whereLambda, params string[] proNames) 61 { 62 List<T> listModifes = db.Set<T>().Where(whereLambda).ToList(); 63 Type t = typeof(T); 64 List<PropertyInfo> proInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList(); 65 Dictionary<string, PropertyInfo> dicPros = new Dictionary<string, PropertyInfo>(); 66 proInfos.ForEach(p => 67 { 68 if (proNames.Contains(p.Name)) 69 { 70 dicPros.Add(p.Name, p); 71 } 72 }); 73 foreach (string proName in proNames) 74 { 75 if (dicPros.ContainsKey(proName)) 76 { 77 PropertyInfo proInfo = dicPros[proName]; 78 object newValue = proInfo.GetValue(model, null); 79 foreach (T m in listModifes) 80 { 81 proInfo.SetValue(m, newValue, null); 82 } 83 } 84 } 85 return db.SaveChanges(); 86 } 87 88 /// <summary> 89 /// 批量修改(支持lambda) 90 /// </summary> 91 /// <param name="model">要修改實體中 修改後的屬性 </param> 92 /// <param name="whereLambda">查詢實體的條件</param> 93 /// <param name="proNames">lambda的形式表示要修改的實體屬性名</param> 94 /// <returns></returns> 95 public int ModifyBy(T model, Expression<Func<T, bool>> whereLambda, params Expression<Func<T, object>>[] proNames) 96 { 97 List<T> listModifes = db.Set<T>().Where(whereLambda).ToList(); 98 Type t = typeof(T); 99 List<PropertyInfo> proInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList(); 100 Dictionary<string, PropertyInfo> dicPros = new Dictionary<string, PropertyInfo>(); 101 if (proNames != null) 102 { 103 foreach (var myProperyExp in proNames) 104 { 105 var my_ProName = new PropertyExpressionParser<T>(myProperyExp).Name; 106 proInfos.ForEach(p => 107 { 108 if (p.Name.Equals(my_ProName)) 109 { 110 dicPros.Add(p.Name, p); 111 } 112 }); 113 if (dicPros.ContainsKey(my_ProName)) 114 { 115 PropertyInfo proInfo = dicPros[my_ProName]; 116 object newValue = proInfo.GetValue(model, null); 117 foreach (T m in listModifes) 118 { 119 proInfo.SetValue(m, newValue, null); 120 } 121 } 122 } 123 } 124 return db.SaveChanges(); 125 } 126
3. EF增刪改封裝(事務分離)
(1). 事務批量處理
1 /// <summary> 2 /// 事務批量處理 3 /// </summary> 4 /// <returns></returns> 5 public int SaveChange() 6 { 7 return db.SaveChanges(); 8 }
(2). 新增
1 /// <summary> 2 /// 新增 3 /// </summary> 4 /// <param name="model">須要新增的實體</param> 5 public void AddNo(T model) 6 { 7 db.Set<T>().Add(model); 8 }
(3). 修改
1 /// <summary> 2 /// 修改 3 /// </summary> 4 /// <param name="model">修改後的實體</param> 5 public void ModifyNo(T model) 6 { 7 db.Entry(model).State = EntityState.Modified; 8 }
(4). 刪除
/// <summary> /// 刪除 /// </summary> /// <param name="model">須要刪除的實體</param> public void DelNo(T model) { db.Entry(model).State = EntityState.Deleted; } /// <summary> /// 條件刪除 /// </summary> /// <param name="delWhere">須要刪除的條件</param> public void DelByNo(Expression<Func<T, bool>> delWhere) { List<T> listDels = db.Set<T>().Where(delWhere).ToList(); listDels.ForEach(d => { db.Set<T>().Attach(d); db.Set<T>().Remove(d); }); }
4. Z.EntityFramework.Extensions 插件封裝
方案一:在使用EF事務分離的方法的前提下,單獨調用提交方法
1 /// <summary> 2 /// 事務提交,速度約爲saveChange的10倍-15倍 3 /// </summary> 4 public void BulkSaveChanges() 5 { 6 db.BulkSaveChanges(); 7 }
方案二:插件特有的增刪改方法
/// <summary> /// 新增 /// </summary> /// <param name="model">新增的實體集合</param> public void BulkInsert(List<T> model) { db.BulkInsert<T>(model); } /// <summary> /// 刪除 /// </summary> /// <param name="model">須要刪除的實體集合</param> public void BulkDelete(List<T> model) { db.BulkDelete<T>(model); } /// <summary> /// 根據條件刪除 /// </summary> /// <param name="delWhere">刪除條件</param> public void BulkDeleteBy(Expression<Func<T, bool>> delWhere) { List<T> listDels = db.Set<T>().Where(delWhere).ToList(); db.BulkDelete<T>(listDels); } /// <summary> /// 須要修改的實體集合 /// </summary> /// <param name="model"></param> public void BulkUpdate(List<T> model) { db.BulkUpdate<T>(model); }
未完,後續會不斷更新。