Пример #1
0
 /// <summary>
 ///
 /// </summary>
 /// <remarks>
 ///
 /// </remarks>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="entity"></param>
 /// <returns></returns>
 public int Update(TEntity entity)
 {
     if (DbContext.Entry(entity).State != EntityState.Detached)
     {
         return(DbContext.SaveChanges());
     }
     DbContext.Set <TEntity>().Attach(entity);
     PropertyInfo[] props = entity.GetType().GetProperties();
     foreach (PropertyInfo prop in props)
     {
         if (prop.GetValue(entity, null) != null)
         {
             if (typeof(IEntity <>).IsAssignableGenericFrom(prop.PropertyType) ||//这里只对当前实体更新,不对实体内部更新
                 typeof(IEntity).IsAssignableGenericFrom(prop.PropertyType) ||
                 typeof(ICollection <>).IsAssignableGenericFrom(prop.PropertyType))
             {
                 continue;
             }
             if (prop.GetValue(entity, null).ToString() == "&nbsp;")
             {
                 DbContext.Entry(entity).Property(prop.Name).CurrentValue = null;
             }
             DbContext.Entry(entity).Property(prop.Name).IsModified = true;
         }
     }
     return(DbContext.SaveChanges());
 }
        protected MyDataTableResponse <T> QueryPaging(int take, int?skip, Func <IQueryable <T>
                                                                                , IQueryable <T> > funcForInclude = null,
                                                      Func <IQueryable <T>, IQueryable <T> > funcForFilter        = null)
        {
            if (take <= 0)
            {
                take = 20;
            }

            if (skip <= 0)
            {
                throw new Exception("skip صفر یا کوچکتر از صفر پاس شده است");
            }

            var entities = _context.Set <T>().AsNoTracking().AsQueryable();

            if (funcForInclude != null)
            {
                entities = funcForInclude(entities);
            }

            if (funcForFilter != null)
            {
                entities = funcForFilter(entities);
            }

            IQueryable <T> res;

            if (skip.HasValue && skip > 0)
            {
                res = entities.OrderByDescending(e => e.Id).Skip(skip.Value).Take(take);
            }
            else
            {
                res = entities.OrderByDescending(e => e.Id).Take(take);
            }

            return(new MyDataTableResponse <T>
            {
                LastSkip = skip,
                LastTake = take,
                List = res.ToList(),
                Total = res.Count(),
            });
        }