Beispiel #1
0
        public bool UpdateSpecificProperty(TEntity entity, params Expression <Func <TEntity, object> >[] propertyExpression)
        {
            using (var ctx = new OpsManageDbContext())
            {
                ctx.Set <TEntity>().Attach(entity);
                foreach (Expression <Func <TEntity, Object> > expression in propertyExpression)
                {
                    ctx.Entry(entity).Property(expression).IsModified = true;
                }

                return(ctx.SaveChanges() > 0);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public bool Add(IList <TEntity> entities)
        {
            var result = false;

            using (var ctx = new OpsManageDbContext())
            {
                ctx.Set <TEntity>().AddRange(entities);
                var effectedRows = ctx.SaveChanges();
                if (effectedRows > 0)
                {
                    result = true;
                }
            }

            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(TEntity entity)
        {
            var result = false;

            using (var ctx = new OpsManageDbContext())
            {
                ctx.Set <TEntity>().Update(entity);
                var effectedRows = ctx.SaveChanges();
                if (effectedRows > 0)
                {
                    result = true;
                }
            }

            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// 删除(按条件)
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public bool Remove(Expression <Func <TEntity, bool> > predicate)
        {
            var result = false;

            using (var ctx = new OpsManageDbContext())
            {
                var items = ctx.Set <TEntity>().Where(predicate).ToList();
                ctx.Set <TEntity>().RemoveRange(items);
                var effectedRows = ctx.SaveChanges();
                if (effectedRows > 0)
                {
                    result = true;
                }
            }

            return(result);
        }