欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

封装自己通用的 增删改查的方法 By EF

程序员文章站 2022-07-04 23:54:19
封装自己的通用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的方法,这就不过多啰嗦了!