Ejemplo n.º 1
0
        public T GetModel(string sql)
        {
            DataSet ds = new DataSet();

            using (var db = new Model.Entities())
            {
                return(db.Set <T>().SqlQuery(sql).FirstOrDefault());
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 批量新增实体
 /// </summary>
 /// <param name="entitys"></param>
 /// <returns></returns>
 public int AddListEntity(List <T> entitys)
 {
     try
     {
         using (DbContext _dbx = new Model.Entities())
         {
             foreach (T entity in entitys)
             {
                 _dbx.Set <T>().Add(entity);
             }
             return(_dbx.SaveChanges());
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 根据条件 删除实体
        /// </summary>
        /// <param name="doWhere"></param>
        /// <returns></returns>
        public int DelBySqlWhere(Expression <Func <T, bool> > doWhere)
        {
            try
            {
                int iret = -1;

                using (DbContext _dbx = new Model.Entities())
                {
                    iret = BatchExtensions.Delete(_dbx.Set <T>().Where(doWhere));
                }
                return(iret);
            }
            catch (Exception ex)
            {
                RecordLog.RecordWarn(ex.ToString());
                throw ex;
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 批量修改
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="dWhere"></param>
 /// <param name="uProName">要修改的列名</param>
 /// <returns></returns>
 public int UpdateListEntity(T entity, Expression <Func <T, bool> > dWhere, params string[] uProName)
 {
     try
     {
         using (DbContext db = new Model.Entities()) //DBContextFactory().GetDbContext()
         {
             //1.查询要修改的数据
             List <T> listModifing = db.Set <T>().Where(dWhere).ToList();
             //2.获取实体类对象
             Type t = typeof(T);
             //3.获取实体类的所有属性
             List <PropertyInfo> pInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();
             //4.创建一个实体属性的字典集合
             Dictionary <string, PropertyInfo> dictPros = new Dictionary <string, PropertyInfo>();
             //5.将实体属性中要修改的属性添加到字典集合中,健:属性名,值:属性对象
             pInfos.ForEach(p =>
             {
                 if (uProName.Contains(p.Name))
                 {
                     dictPros.Add(p.Name, p);
                 }
             });
             //6.循环要修改的属性名
             foreach (string proName in uProName)
             {
                 //如果存在则取出要修改的属性
                 PropertyInfo proInfo = dictPros[proName];
                 //取出要修改的值
                 object newValue = proInfo.GetValue(entity, null);
                 //批量设置要修改对象的属性
                 foreach (T su in listModifing)
                 {
                     //为要修改的对象属性设置新的值
                     proInfo.SetValue(su, newValue, null);
                 }
             }
             return(db.SaveChanges());
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// 根据条件 删除实体
        /// </summary>
        /// <param name="doWhere"></param>
        /// <returns></returns>
        public int DelByWhere(Expression <Func <T, bool> > doWhere)
        {
            try
            {
                //  List<T> pDels = _db.Set<T>().Where<T>(doWhere).ToList();
                ////List<T> pDels = _db.Set<T>().Where<T>(doWhere).AsQueryable();
                //  pDels.ForEach(
                //      //p => _db.Set<T>().Remove(p)
                //       p =>
                //       {
                //           _db.Entry<T>(p).State = EntityState.Detached;//oracle数据库没这一句不能修改
                //           _db.Set<T>().Attach(p);
                //          _db.Entry<T>(p).State = EntityState.Deleted;
                //       }
                //      );
                //  return _db.SaveChanges();



                int iret = -1;
                //Logger("根据条件删除" + typeof(T).Name + "中的数据", () =>
                //{
                using (DbContext _dbx = new Model.Entities())
                {
                    List <T> listDels = _dbx.Set <T>().Where(doWhere).ToList();

                    listDels.ForEach(d =>
                    {
                        _dbx.Set <T>().Attach(d);
                        _dbx.Set <T>().Remove(d);
                    });
                    iret = _dbx.SaveChanges();

                    //});
                }
                return(iret);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 4.x扩展修改方法(把不须要修改的列用LAMBDA数组表示出来)
        /// </summary>
        /// <param name="model">要修改的实体对象</param>
        /// <param name="ignoreProperties">不须要修改的相关字段</param>
        /// <returns>受影响的行数</returns>
        public int UpdateEntity(T model, params Expression <Func <T, object> >[] ignoreProperties)
        {
            int iret = -1;

            //Logger("修改" + typeof(T).Name + "中的数据", () =>
            //{
            using (DbContext db = new Model.Entities()) //DBContextFactory().GetDbContext()
            {
                db.Entry <T>(model).State = EntityState.Detached;
                db.Set <T>().Attach(model);

                DbEntityEntry entry = db.Entry <T>(model);
                entry.State = System.Data.EntityState.Unchanged;

                Type t = typeof(T);
                List <PropertyInfo> proInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();

                Dictionary <string, PropertyInfo> dicPros = new Dictionary <string, PropertyInfo>();
                proInfos.ForEach(
                    p => dicPros.Add(p.Name, p)
                    );

                if (ignoreProperties != null)
                {
                    foreach (var ignorePropertyExpression in ignoreProperties)
                    {
                        //根据表达式得到对应的字段信息
                        var ignorePropertyName = new PropertyExpressionParser <T>(ignorePropertyExpression).Name;
                        dicPros.Remove(ignorePropertyName);
                    }
                }

                foreach (string proName in dicPros.Keys)
                {
                    entry.Property(proName).IsModified = true;
                }
                iret = db.SaveChanges();
            }
            return(iret);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 修改实
        /// </summary>
        /// <param name="entity">修改对象</param>
        /// <param name="pNas">要修改的属性名称集体</param>
        /// <returns></returns>
        public int UpdateEntity(T entity, params string[] pNas)
        {
            try
            {
                using (DbContext _dbx = new Model.Entities())
                {
                    _dbx.Entry <T>(entity).State = EntityState.Detached;//oracle数据库没这一句不能修改

                    DbEntityEntry upEntity = _dbx.Entry <T>(entity);
                    upEntity.State = System.Data.EntityState.Unchanged;
                    foreach (string pNa in pNas)
                    {
                        upEntity.Property(pNa).IsModified = true;
                    }
                    return(_dbx.SaveChanges());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 新增实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int AddEntity(T entity)
        {
            try
            {
                using (DbContext _dbx = new Model.Entities())
                {
                    _dbx.Set <T>().Add(entity);
                    //_dbx.Configuration.ValidateOnSaveEnabled = false;
                    int count = _dbx.SaveChanges();
                    //_dbx.Configuration.ValidateOnSaveEnabled = true;
                    return(count);
                }


                //DbSet<T> dst = _db.Set<T>();
                //dst.Add(entity);
                //return _db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }