Package own generic CURD By EF
the System the using; the using the System.Collections.Generic; the using to System.Data.Entity; the using System.Data.Entity.Infrastructure; the using the System.Linq; the using System.Linq.Expressions; the using the System.Text; the using the MODEL; namespace ZRFDEMO.DAL { public class BaseDAL <T> T the wHERE: // ---- class where we can achieve a new generic interfaces such as: IbaseDAL <T> ....... { . 1 // create EF context ZRFEntityes db = new ZRFEntityes (); // --- here we can use to optimize thread of unique, public BaseDAL () { // Close inspection ef db.Configuration.ValidateOnSaveEnabled = to false; } #region 0.0 EF batch update data to the container database + int SaveChanges () /// <Summary> /// 0.0 EF batch updates to the database container data /// </ Summary> /// <Returns> Returns the number of rows affected </ Returns> public int the SaveChanges () { return db.SaveChanges (); } #endregion #region new method 1.0 + void Add (T model ) /// <Summary> /// 1.0 new method /// </ Summary> /// <param name = "Model"> </ param> public void the Add (Model T) { //. 1. directly EF method to obtain a context for the Set T to a target class does DbSet operation // var db.Set DbSet = <T> (); //dbSet.Add(model); . db.Set <T> () the Add ( Model); } #endregion #region 2.0 the Delete void Delete method + (Model T) /// <Summary> /// 2.0 erasing method /// </ Summary> /// <param name = "Model"> </ param> public void the Delete ( Model T) { DbEntityEntry entry db.Entry = <T> (Model); entry.State = System.Data.EntityState.Deleted; } #endregion #region 2.1 deleting method + void DeleteBy (System.Linq.Expressions.Expression < FUNC <T, BOOL >> delWhere) /// <the Summary> /// 2.1 condition deletion method /// </ the Summary> /// <param name = "delWhere"> element of the query to be deleted </ 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); the else } return db.Set<T>().Where(whereLambda).OrderByDescending(keySelector); } #endregion #region query methods 4.2 - + IQueryable with the Include <T> WhereInclude /// <Summary> /// Query Method 4.2 - with the Include /// </ Summary> /// <param name = "whereLambda"> < / param> /// <param name = "includePropertyNames"> to join query attribute name </ param> /// <Returns> </ Returns> public IQueryable <T> WhereInclude (the Expression <Func <T, BOOL> > whereLambda, the params String [] includePropertyNames) { dbquery <T> = db.Set dbquery <T> (); the 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,>> keySelector TKey, BOOL = isAsc to true, the params String [] includePropertyNames) // 0. Acquires the data table corresponding to the query object to be operated { dbquery <T> = db.Set dbquery <T> (); //1.include properties of the foreach (String includeName in includePropertyNames) { dbquery = dbQuery.Include (includeName ); } IOrderedQueryable <T> orderQuery = null; . // Sort 2 IF (isAsc) orderQuery = dbQuery.OrderBy (keySelector); the else orderQuery = dbQuery.OrderByDescending (keySelector); .. 3 // paging query pagedData.ListData = orderQuery .Where (whereLambda) .skip ((pagedData.PageIndex -. 1) * pagedData.PageSize) .Take (pagedData.PageSize) .ToList (); .. 4 // Get the number of rows pagedData.RowCount = orderQuery.Where(whereLambda).Count(); } #Endregion } }
In addition, we can also add Sql query method, which but more long-winded!