三层框架 自动BaseDal

baseDal类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Linq.Dynamic;
using System.Data.Entity.Infrastructure;

namespace BookShopPlus2.EfDal
{
    /// <summary>
    /// 封装增删改的类
    /// </summary>
    public class BaseDal<T>
        where T:class
    {
    //实例化数据操作上下文
        public BookShopPlus2Context DbContext = new BookShopPlus2Context();

        //创建无参构造函数
        public BaseDal()
        {
        //关闭四个功能
            //关闭自动检查更新项
            DbContext.Configuration.AutoDetectChangesEnabled = false;
            //关闭懒加载
            DbContext.Configuration.LazyLoadingEnabled = false;
            //关闭动态代理
            DbContext.Configuration.ProxyCreationEnabled = false;
            //关闭检查机制(非空验证)
            DbContext.Configuration.ValidateOnSaveEnabled = false;
        }
        #region 增删改

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="modle"></param>
        /// <returns></returns>
        public int Add(T modle)
        {
            try
            {
                DbContext.Set<T>().Attach(modle);
                //更改状态[添加]
                DbContext.Entry(modle).State = EntityState.Added;
                //SaveChanges返回受影响的行数,增删改查都要用到此代码
                int rows = DbContext.SaveChanges();
                return rows;
            }
            catch (Exception)
            {
                return -1;
            }
            finally
            {
                //移除对象池
                DbContext.Entry(modle).State = EntityState.Detached;
            }

        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="modle"></param>
        /// <returns></returns>
        public int Update(T modle)
        {
            try
            {
                DbContext.Set<T>().Attach(modle);
                //更改状态[添加]
                DbContext.Entry(modle).State = EntityState.Modified;
                //SaveChanges返回受影响的行数,增删改查都要用到此代码
                int rows = DbContext.SaveChanges();
                return rows;
            }
            catch (Exception)
            {
                return -1;
            }
            finally
            {
                //移除对象池
                DbContext.Entry(modle).State = EntityState.Detached;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="modle"></param>
        /// <returns></returns>
        public int Dal(T modle)
        {
            try
            {
                DbContext.Set<T>().Attach(modle);
                //更改状态[添加]
                DbContext.Entry(modle).State = EntityState.Deleted;
                //SaveChanges返回受影响的行数,增删改查都要用到此代码
                int rows = DbContext.SaveChanges();
                return rows;
            }
            catch (Exception)
            {
                return -1;
            }


        }
        
        #endregion

        #region 查询
        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="joinModel"></param>
        /// <returns></returns>

        public List<T> Select(string condition, params string[] joinModel)
        {
            List<T> list = null;
            //基础语句
            //dbContext.Users.AsNoTracking()
           
            DbQuery<T> baseQuery = DbContext.Set<T>().AsNoTracking();
            //数组可以循环
            for (int i = 0; i < joinModel.Length; i++)
            {
                //dbContext.Users.AsNoTracking().Include("UserRole").include("UserStatus")
                baseQuery = baseQuery.Include(joinModel[i]);
            }


            //有条件,加上条件查询
            if (!string.IsNullOrEmpty(condition))
            {
                list = baseQuery.Where(condition).ToList();
            }
            else
            {
                //没有条件直接查询
                list = baseQuery.ToList();
            }
            return list;
        }

       
        /// <summary>
        /// 查询单个对象
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public T SelectOne(string condition, params string[] joinModel)
        {
            T modle = null;
           DbQuery<T> baseQuery= DbContext.Set<T>().AsNoTracking();
           foreach (string join in joinModel)
          { 
               baseQuery = baseQuery.Include(join);
          }
            if (!string.IsNullOrEmpty(condition))
            {
                modle = baseQuery.Where(condition).FirstOrDefault();
            }
            else
            {
                //没有条件直接查询
                modle = baseQuery.FirstOrDefault();
            }
            return modle;
        }

        /// <summary>
        /// 翻页方法
        /// </summary>
        /// <param name="totalCount">总条数</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">提取条数</param>
        /// <param name="orderName">排序列名</param>
        /// <param name="condition">条件</param>
        /// <param name="joinModels">导航属性</param>params 表示可填可不填
        /// <returns></returns>
        public List<T> SelectByPage(out int totalCount, int currentPage, int pageSize, string orderName, string condition, params string[] joinModels)
        {
            //参数(out int 总条数,int 当前页,int 提取条数,string order排序列名,string 条件,string[]joinModels(导航属性-外键))
        //dbContext.Books.AsNotracking(),Incluide(),Orderby(),where(b=> 1==1).Skip().Take().ToList();
           DbQuery<T> baseQuery= DbContext.Set<T>().AsNoTracking();
            foreach (string join in joinModels)
            {
                baseQuery = baseQuery.Include(join);
            }
            //拼接OrderBy
           IQueryable<T> orderQuery= baseQuery.OrderBy(orderName);
            //拼接where
           if (!string.IsNullOrEmpty(condition))
           {
               orderQuery = orderQuery.Where(condition);
               //总条数
               totalCount = orderQuery.Count();
           }
           else
           {
               totalCount = orderQuery.Count();
           }
           return orderQuery.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();

        }
        #endregion
        
    }
}
原文地址:https://www.cnblogs.com/x666066/p/10267030.html