Beispiel #1
0
 /// <summary>
 /// 更新
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public async Task <bool> UpdateAsync(TEntity entity)
 {
     using (var ctx = new OpsManageDbContext())
     {
         ctx.Set <TEntity>().Update(entity);
         return(await ctx.SaveChangesAsync() > 0);
     }
 }
Beispiel #2
0
        /// <summary>
        /// 删除(按条件)
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task <bool> RemoveAsync(Expression <Func <TEntity, bool> > predicate)
        {
            using (var ctx = new OpsManageDbContext())
            {
                var entity = ctx.Set <TEntity>();
                var items  = await entity.Where(predicate).ToListAsync();

                entity.RemoveRange(items);
                return(await ctx.SaveChangesAsync() > 0);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 跟新指定的字段
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="propertyExpression"></param>
        /// <returns></returns>
        public async Task <bool> UpdateSpecificPropertyAsync(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(await ctx.SaveChangesAsync() > 0);
            }
        }