zoukankan      html  css  js  c++  java
  • EF泛型查询

    EF泛型查询

    参考地址

    栗子

    后期可优化

        public class EFHelper
        {
            //依赖注入
            readonly DBContext _db;
            public EFHelper(DBContext dbContext)
            {
                _db = dbContext;
            }
    
    
            public virtual IQueryable<TEntity> Quyery<TEntity>() where TEntity : class
            {
                return _db.Set<TEntity>();
            }
    
    
            /// <summary>
            /// 查询单个实体
            /// </summary>
            /// <typeparam name="TEntity"></typeparam>
            /// <param name="pre"></param>
            /// <returns></returns>
            public virtual IQueryable<TEntity> Quyery<TEntity>(Expression<Func<TEntity, bool>> pre) where TEntity : class
            {
    
                return _db.Set<TEntity>().Where(pre);
            }
    
    
            /// <summary>
            /// 根据ID查询单个实体
            /// </summary>
            /// <typeparam name="TEntity"></typeparam>
            /// <param name="pre"></param>
            /// <returns></returns>
            public virtual TEntity QuyeryByID<TEntity>(int id) where TEntity : BaseEntity
            {
                return _db.Set<TEntity>().Where(a => a.ID.Equals(id)).First();
            }
    
            /// <summary>
            /// 添加实体
            /// </summary>
            /// <typeparam name="TEntity"></typeparam>
            /// <param name="entity"></param>
            public virtual void Add<TEntity>(TEntity entity) where TEntity : BaseEntity
            {
                _db.Set<TEntity>().Add(entity);
            }
    
            /// <summary>
            /// 批量添加实体
            /// </summary>
            /// <typeparam name="TEntity"></typeparam>
            /// <param name="listEntity"></param>
            public virtual void Add<TEntity>(List<TEntity> listEntity) where TEntity : BaseEntity
            {
                _db.Set<TEntity>().AddRange(listEntity);
            }
    
         
            /// <summary>
            /// 根据传入条件删除数据
            /// </summary>
            /// <typeparam name="TEntity"></typeparam>
            /// <param name="pre"></param>
            public virtual void Delete<TEntity>(Expression<Func<TEntity, bool>> pre) where TEntity : class
            {
                var dele = this.Quyery(pre).FirstOrDefault();
                if(dele!=null)
                    this.Delete(dele);
            }
            /// <summary>
            /// 根据实体标记删除
            /// </summary>
            /// <typeparam name="TEntity"></typeparam>
            /// <param name="entity"></param>
            public virtual void Delete<TEntity>(TEntity entity) where TEntity : class
            {
                _db.Set<TEntity>().Remove(entity);
            }
            /// <summary>
            /// 批量标记删除
            /// </summary>
            /// <typeparam name="TEntity"></typeparam>
            /// <param name="listEntity"></param>
            public virtual void Delete<TEntity>(List<TEntity> listEntity) where TEntity : BaseEntity
            {
                _db.Set<TEntity>().RemoveRange(listEntity);
            }
    
           
    
            /// <summary>
            /// 保存数据到数据库
            /// </summary>
            public virtual void SaveChanges()
            {
                _db.SaveChanges();
                _db.Dispose();
            }
        }
    }
    
  • 相关阅读:
    从徐飞的文章《Web应用的组件化开发(一)中窥视web应用开发的历史
    【转载】开发者眼中的Spring与Java EE
    matplotlib.pyplot画图包的使用简介 (4) [柱状图]
    matplotlib.pyplot画图包的使用简介 (3) [折线图]
    matplotlib.pyplot画图包的使用简介 (2) [散点图]
    matplotlib.pyplot画图包的使用简介 (1)
    自定义代码实现简单的多元一次线性函数的随机梯度下降
    ajax请求模板
    django的{{}}与js的{{}}冲突解决
    django配置mysql
  • 原文地址:https://www.cnblogs.com/Alex-Mercer/p/13354480.html
Copyright © 2011-2022 走看看