封裝自己的通用CURD By EF ...
封裝自己的通用CURD By EF
using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using System.Linq.Expressions; using System.Text; using MODEL; namespace ZRFDEMO.DAL { public class BaseDAL<T> where T:class //----這裡我們可以實現 新增一個泛型的介面 如: IbaseDAL<T>....... { //1.創建EF上下文 ZRFEntityes db = new ZRFEntityes();//---這裡我們可以使用線程內唯一來優化, public BaseDAL() { //關閉 ef 檢查 db.Configuration.ValidateOnSaveEnabled = false; } #region 0.0 批量更新EF容器數據到資料庫 +int SaveChanges() /// <summary> /// 0.0 批量更新EF容器數據到資料庫 /// </summary> /// <returns>返回受影響行數</returns> public int SaveChanges() { return db.SaveChanges(); } #endregion #region 1.0 新增方法 +void Add(T model) /// <summary> /// 1.0 新增方法 /// </summary> /// <param name="model"></param> public void Add(T model) { //1.直接通過EF上下文的 Set方法 獲取一個 針對於 T類 做操作的 DbSet對象 //var dbSet = db.Set<T>(); //dbSet.Add(model); db.Set<T>().Add(model); } #endregion #region 2.0 刪除方法 +void Delete(T model) /// <summary> /// 2.0 刪除方法 /// </summary> /// <param name="model"></param> public void Delete(T model) { DbEntityEntry entry = db.Entry<T>(model); entry.State = System.Data.EntityState.Deleted; } #endregion #region 2.1 條件刪除方法 +void DeleteBy(System.Linq.Expressions.Expression<Func<T, bool>> delWhere) /// <summary> /// 2.1 條件刪除方法 /// </summary> /// <param name="delWhere">要刪除的元素查詢條件</param> public void DeleteBy(System.Linq.Expressions.Expression<Func<T, bool>> delWhere) { var delList = db.Set<T>().Where(delWhere); foreach (T model in delList) { Delete(model); } } #endregion #region 3.0 修改實體 + void Modify(T model, params string[] propertyNames) /// <summary> /// 3.0 修改實體 /// </summary> /// <param name="model"></param> /// <param name="propertyNames"></param> public void Modify(T model, params string[] propertyNames) { DbEntityEntry entry = db.Entry<T>(model); entry.State = System.Data.EntityState.Unchanged; foreach (string proName in propertyNames) { entry.Property(proName).IsModified = true; } } #endregion #region 4.0 查詢方法 +IQueryable<T> Where(Expression<Func<T, bool>> whereLambda) /// <summary> /// 4.0 查詢方法 /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public IQueryable<T> Where(Expression<Func<T, bool>> whereLambda) { return db.Set<T>().Where(whereLambda); } #endregion #region 4.1 查詢方法 -帶排序 +IQueryable<T> WhereOrder<TKey> /// <summary> /// 4.1 查詢方法 -帶排序 /// </summary> /// <typeparam name="TKey"></typeparam> /// <param name="whereLambda"></param> /// <param name="keySelector">u=></param> /// <param name="isAsc"></param> /// <returns></returns> public IQueryable<T> WhereOrder<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> keySelector, bool isAsc = true) { if (isAsc) return db.Set<T>().Where(whereLambda).OrderBy(keySelector); else return db.Set<T>().Where(whereLambda).OrderByDescending(keySelector); } #endregion #region 4.2 查詢方法 -帶Include +IQueryable<T> WhereInclude /// <summary> /// 4.2 查詢方法 -帶Include /// </summary> /// <param name="whereLambda"></param> /// <param name="includePropertyNames">要進行連接查詢的 屬性名</param> /// <returns></returns> public IQueryable<T> WhereInclude(Expression<Func<T, bool>> whereLambda, params string[] includePropertyNames) { DbQuery<T> dbQuery = db.Set<T>(); foreach (string includeName in includePropertyNames) { dbQuery = dbQuery.Include(includeName); } return dbQuery.Where(whereLambda); //DbQuery<T> dbSet = (DbQuery<T>)db.Set<T>().Where(whereLambda); //foreach (string includeName in includePropertyNames) //{ // dbSet = dbSet.Include(includeName); //} //return dbSet; } #endregion #region 4.3 查詢方法 -帶Include 和 排序 +IQueryable<T> WhereInclude<TKey> /// <summary> /// 4.3 查詢方法 -帶Include 和 排序 /// </summary> /// <typeparam name="TKey"></typeparam> /// <param name="whereLambda"></param> /// <param name="keySelector"></param> /// <param name="isAsc"></param> /// <param name="includePropertyNames"></param> /// <returns></returns> public IQueryable<T> WhereInclude<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> keySelector, bool isAsc = true, params string[] includePropertyNames) { DbQuery<T> dbQuery = db.Set<T>(); foreach (string includeName in includePropertyNames) { dbQuery = dbQuery.Include(includeName); } IQueryable<T> query = dbQuery.Where(whereLambda); if (isAsc) return query.OrderBy(keySelector); else return query.OrderByDescending(keySelector); } #endregion #region 4.4 查詢方法 - 分頁+Include+排序 + void WherePaged<TKey> /// <summary> /// 4.4 查詢方法 - 分頁+Include+排序 /// </summary> /// <typeparam name="TKey"></typeparam> /// <param name="pagedData"></param> /// <param name="whereLambda"></param> /// <param name="keySelector"></param> /// <param name="isAsc"></param> /// <param name="includePropertyNames"></param> public void WherePaged<TKey>(MODEL.FormatMODEL.PagedData pagedData, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> keySelector, bool isAsc = true, params string[] includePropertyNames) { //0.獲取 要操作的 數據表 對應的查詢對象 DbQuery<T> dbQuery = db.Set<T>(); //1.include 屬性 foreach (string includeName in includePropertyNames) { dbQuery = dbQuery.Include(includeName); } IOrderedQueryable<T> orderQuery = null; //2.排序 if (isAsc) orderQuery = dbQuery.OrderBy(keySelector); else orderQuery = dbQuery.OrderByDescending(keySelector); //3.分頁查詢 pagedData.ListData = orderQuery.Where(whereLambda).Skip((pagedData.PageIndex - 1) * pagedData.PageSize).Take(pagedData.PageSize).ToList(); //4.獲取總行數 pagedData.RowCount = orderQuery.Where(whereLambda).Count(); } #endregion } }
另外我們還可新增查詢Sql的方法,這就不過多啰嗦了!