1.項目中常常要用到 EF,有時候大多數的增刪改查都是重複性的東西,本次封裝就是爲了快速開發,期間沒有考慮到架構上的各類思想,就感受到欠缺點什麼東西因此此次將這些拉出來,有存在問題的話還請各位多多指導。mysql
2.封裝後從壓力和併發上也沒有去測試,有興趣的小夥伴還望給看下。好了不廢話了直接上了。
a.先看下大概結構以下 按照順序介紹 a.1: 實體就是通常你們手動寫的實體
a.2: DALContext.cs 代碼以下:sql
namespace Test.Web.Site.DAL { public class DALContext<T> : DbContext where T : class { public DALContext(string con) : base(con){} public DALContext(){} public DbSet<T> TV { get; set; } } }
a.3 BaseDAL.cs 主要的增刪改方法數據庫
namespace Test.Web.Site.DAL { public class BaseDAL<TEntity> where TEntity : class,new() { private static readonly object s_lock = new object(); private static string constr = "xxxConn";//默認一個字符串 public BaseDAL(string con = "") { if (con != constr && con.Length > 0) { lock (s_lock) { constr = con; } } }
#region Modify and Delete public virtual bool Insert(TEntity entity) { using (var db = new DALContext<TEntity>(constr)) { db.Set<TEntity>().Add(entity); return db.SaveChanges() > 0; } } public virtual bool Delete(object col) { using (var db = new DALContext<TEntity>(constr)) { TEntity entityToDelete = db.Set<TEntity>().Find(col); if (entityToDelete != null) { return Delete(entityToDelete); } else { return false; } } } public virtual bool Delete(Expression<Func<TEntity, bool>> predicate) { TEntity entityToDelete = Get(predicate); if (entityToDelete != null) { return Delete(entityToDelete); } else { return false; } } public virtual IEnumerable<TEntity> InsertAll(List<TEntity> list) { using (var db = new DALContext<TEntity>(constr)) { var dbSet = db.Set<TEntity>(); List<TEntity> tList = new List<TEntity>(); foreach (var item in list) { try { db.Set<TEntity>().Add(item); } catch (Exception) { tList.Add(item); throw; } } db.SaveChanges(); return tList; } } public virtual IEnumerable<TEntity> UpdateAll(List<TEntity> list) { using (var db = new DALContext<TEntity>(constr)) { var dbSet = db.Set<TEntity>(); List<TEntity> tList = new List<TEntity>(); foreach (var item in list) { try { var entity = dbSet.Attach(item); db.Entry(item).State = System.Data.EntityState.Modified; } catch (Exception) { tList.Add(item); throw; } } db.SaveChanges(); return tList; } } public virtual bool Update(TEntity entityToUpdate) { using (var db = new DALContext<TEntity>(constr)) { var dbSet = db.Set<TEntity>(); var entity = dbSet.Attach(entityToUpdate); db.Entry(entityToUpdate).State = System.Data.EntityState.Modified; return db.SaveChanges() > 0; } } #endregion } }
a.4 DatabaseExtensions.cs 一個EF 擴展類 用於 鏈接其餘數據庫好比 mysql(參考的網絡資源),執行sql語句查詢視圖有須要可在下方評論,我會及時回覆網絡
a.5 : Test.Web.Site.BLL 對Control 開放的一系列方法 ,這裏僅列舉了部分代碼出來,若有須要可在下方評論我及時回覆你。架構
namespace Test.Web.Site.BLL { public class DataBLL<T> where T : class,new() { // 鏈接字符串 public DataBLL(string constr = "") { SingletonBase<BaseDAL<T>>.Initialize(new BaseDAL<T>(constr)); }public T Get(Expression<Func<T, bool>> filter, string includeProperties = "") { return SingletonBase<BaseDAL<T>>.Instance.Get(filter, includeProperties); } // 插入一個實體 public bool AddEntity(T t_entity) { return SingletonBase<BaseDAL<T>>.Instance.Insert(t_entity); } // 跟新實體 public bool Update(T t_enttiy) { return SingletonBase<BaseDAL<T>>.Instance.Update(t_enttiy); } // 依據sql查詢 list public IEnumerable<object> GetListBySql(string query,bool otherdb = true) { return SingletonBase<BaseDAL<T>>.Instance.GetBySql(query, otherdb); } // 刪除知足條件的全部數據 public IEnumerable<T> DeleteAll(Expression<Func<T, bool>> predicate) { return SingletonBase<BaseDAL<T>>.Instance.DeleteAll(predicate); } // 批量更新 list public IEnumerable<T> UpdateAll(List<T> list) { return SingletonBase<BaseDAL<T>>.Instance.UpdateAll(list); } // 批量添加 public IEnumerable<T> InsertAll(List<T> list) { return SingletonBase<BaseDAL<T>>.Instance.InsertAll(list); } } }
3.Control 調用只須要相似:併發
Account currAccount = new DataBLL<Account>(ConfigHelper.Constr_read).Get(p => p.UserId == 1111);
new DataBLL<Account>.GetPage(out totalCount, query.Expression, pageIndex, pageSize, k => k.OrderByDescending(_ => _.CreateTime)).ToList();測試
總結:到此就結束了全部代碼可複製過去加入EF引用就開始用了。有發現問題的小夥伴敬請拋磚。小弟不勝感激。spa