码迷,mamicode.com
首页 > 其他好文 > 详细

EF增删查改基类

时间:2017-03-18 00:41:17      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:.exe   on()   dbconnect   end   默认   状态   ctc   string   objectc   

 /// <summary>
     /// EF DAL CURD基类
     /// </summary>
     /// <typeparam name="T"></typeparam>
     public class BaseDAL<T> where T : class, new()
     {
  
         /// <summary>
         /// 上下文网关
         /// </summary>
         protected SchoolEntities db = new SchoolEntities();
  
         #region 1.Add
  
        
         /// <summary>
         /// 增加一条数据
         /// </summary>
         /// <param name="entity"></param>
         /// <returns></returns>
         public T AddEntity(T entity)
         {
  
             db.Entry<T>(entity).State = EntityState.Added;
            // db.Set<T>().Add(entity);此方法同上方法
             db.SaveChanges();
             return entity;//因为可能要返回自动增长的ID,所以把整个实体返回,否则可以直接返回bool。
         }
         /// <summary>
         /// 同时增加多条数据到一张表(事务处理)
         /// </summary>
         /// <param name="entitys"></param>
         /// <returns></returns>
         public bool AddEntity(List<T> entitys)
         {
             foreach (var entity in entitys)
             {
                 db.Entry<T>(entity).State = EntityState.Added;
             }
            // entitys.ForEach(c=>db.Entry<T>(c).State = EntityState.Added);//等价于上面的循环
             return db.SaveChanges() > 0;
         }
         #endregion
  
         #region 2.Modify
         /// <summary>
         /// 修改一条数据,会修改所有列的值,没有赋值的属性将会被赋予属性类型的默认值**************
         /// </summary>
         /// <param name="entity"></param>
         /// <returns></returns>
         public bool ModifyEntity(T entity)
         {
             db.Set<T>().Attach(entity);
             db.Entry<T>(entity).State = EntityState.Modified;//将所有属性标记为修改状态
             return db.SaveChanges() > 0;
         }
         /// <summary>
         /// 修改一条数据,会修改指定列的值
         /// </summary>
         /// <param name="entity">要修改的实体对象</param>
         /// <param name="proNames">要修改的属性名称</param>
         /// <returns></returns>
         public bool ModifyEntity(T entity, params string[] proNames)
         {
             db.Set<T>().Attach(entity);
             DbEntityEntry<T> dbee = db.Entry<T>(entity);
             dbee.State = EntityState.Unchanged;//先将所有属性状态标记为未修改
             proNames.ToList().ForEach(c => dbee.Property(c).IsModified = true);//将要修改的属性状态标记为修改
             return db.SaveChanges() > 0;
         }
         /// <summary>
         /// 根据条件批量修改指定的列********************
         /// </summary>
         /// <param name="entity"></param>
         /// <param name="whereLambds"></param>
         /// <param name="proNames"></param>
         /// <returns></returns>
         public bool ModifyEntity(T entity, Func<T, bool> whereLambds, params string[] proNames)
         {
             var entitys = db.Set<T>().Where(whereLambds).ToList();
             PropertyInfo[] proinfos = entity.GetType().GetProperties();
             List<PropertyInfo> list = new List<PropertyInfo>();
             foreach (var p in proinfos)
             {
                 if (proNames.Contains(p.Name))
                 {
                     list.Add(p);
                 }
             }
             entitys.ForEach(c => {
                 foreach (var p in list)
                 {
                    object value = p.GetValue(entity, null);
                    p.SetValue(c, value, null);
                 }
             });
             return db.SaveChanges() > 0;
        }
        #endregion
  
        #region 3.Delete
      
        /// <summary>
        /// 删除一个实体对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DeleteEntity(T entity)
        {
            db.Set<T>().Attach(entity);
            db.Entry<T>(entity).State = EntityState.Deleted;
            return db.SaveChanges() > 0;
        }
        /// <summary>
        /// 根据条件批量删除实体对象
        /// </summary>
        /// <param name="whereLambds"></param>
        /// <returns></returns>
        public bool DeleteEntityByWhere(Func<T, bool> whereLambds)
        {
            var data = db.Set<T>().Where<T>(whereLambds).ToList();
            return DeleteEntitys(data);     
        }
        /// <summary>
        /// 事务批量删除实体对象
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        public bool DeleteEntitys(List<T> entitys)
        {
            foreach (var item in entitys)
            {
                db.Set<T>().Attach(item);
                db.Entry<T>(item).State = EntityState.Deleted;
            }
            return db.SaveChanges() > 0;
        }
      
        #endregion
  
        #region 4.Select
        //带条件查询
        public IList<T> GetEntitys(Func<T, bool> whereLambds)
        {
            return db.Set<T>().Where<T>(whereLambds).ToList<T>();
        }
        //带排序查询
        public IList<T> GetEntitys<S>(Func<T, bool> whereLambds, bool isAsc, Func<T, S> orderByLambds)
        {
            var temp = db.Set<T>().Where<T>(whereLambds);
            if (isAsc)
            {
                return temp.OrderBy<T, S>(orderByLambds).ToList<T>();
            }
            else
            {
                return temp.OrderByDescending<T, S>(orderByLambds).ToList<T>();
            }
        }
        //带分页查询
        public IList<T> GetPagedEntitys<S>(int pageIndex, int pageSize, out int rows, out int totalPage, Func<T, bool> whereLambds, bool isAsc, Func<T, S> orderByLambds)
       {
           var temp = db.Set<T>().Where<T>(whereLambds);
           rows = temp.Count();
           if (rows % pageSize == 0)
           {
               totalPage = rows / pageSize;
           }
           else
           {
               totalPage = rows / pageSize + 1;
           }
           if (isAsc)
           {
               temp = temp.OrderBy<T, S>(orderByLambds);
           }
           else
           {
               temp = temp.OrderByDescending<T, S>(orderByLambds);
           }
           temp = temp.Skip<T>(pageSize* (pageIndex - 1)).Take<T>(pageSize);

           return temp.ToList<T>();
       }
       //传统sql结合EF分页实现查询
       public IList<T> GetPagedEntitys(int pageIndex, int pageSize, out int rows, out int totalPage, string sql, string where, bool isAsc, string orderKey)
       {

           sql = sql + " where 1=1 " + where;
           sql += " order by " + orderKey;
           if (!isAsc)
           {
               sql += " desc";
           }
           var temp = db.Database.SqlQuery<T>(sql);
           rows = temp.Count();
           if (rows % pageSize == 0)
           {
               totalPage = rows / pageSize;
           }
           else
           {
               totalPage = rows / pageSize + 1;
           }

           temp = temp.Skip(pageSize* (pageIndex - 1)).Take(pageSize);
           return temp.ToList<T>(); ;

       }
       #endregion

       #region 5.显式Tran
       /// <summary>
       /// 显式执行事务
       /// </summary>
       /// <param name="dics"></param>
       /// <returns></returns>
       public int ExeTran(IDictionary<string, DbParameter[]> dics)
       {
           int result = 0;
           DbConnection con = ((IObjectContextAdapter)db).ObjectContext.Connection;
           using (DbTransaction tran = con.BeginTransaction())
           {
               try
               {
                   //第一种,传统的执行事务的方法
                   //db.Database.ExecuteSqlCommand();
                   // db.Database.SqlQuery();                  
                   foreach (var dic in dics)
                   {
                       if (dic.Value != null)
                       {
                           result += db.Database.ExecuteSqlCommand(dic.Key, dic.Value);
                       }
                       else
                       {
                           result += db.Database.ExecuteSqlCommand(dic.Key, dic.Value);
                       }

                   }
                   //第二种,
                   // db.UserInfo.Add(entity);
                   //db.UserInfo.Attach(entity);
                   //db.

                   tran.Commit();
                   return result;

               }
               catch (Exception ex)
               {

                   tran.Rollback();
                   throw ex;
               }
               finally
               {
                   con.Close();
               }
           }
       }
       #endregion
   }

 

EF增删查改基类

标签:.exe   on()   dbconnect   end   默认   状态   ctc   string   objectc   

原文地址:http://www.cnblogs.com/fuqiang88/p/6569246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!