Пример #1
0
        public IList <T> FindAll <T, S>(Expression <Func <T, bool> > conditions, Expression <Func <T, S> > orderBy, int pageSize, int pageIndex, out long total) where T : BaseModel
        {
            using (HContext dbContext = new HContext())
            {
                IQueryable <T> source = conditions == null?dbContext.Set <T>() : dbContext.Set <T>().Where(conditions);

                total = source == null ? 0 : source.Count();
                return(source.OrderByDescending <T, S>(orderBy).ToPagedList(pageIndex, pageSize).ToList <T>());
            }
        }
Пример #2
0
        //private static Random random = new Random();
        #region 扩展基本方法

        public T Find <T>(params object[] keyValues) where T : BaseModel
        {
            using (HContext dbContext = new HContext())
            {
                return(dbContext.Set <T>().Find(keyValues));
            }
        }
Пример #3
0
 public IList <T> FindAllWithoutPaging <T>(Expression <Func <T, bool> > conditions = null) where T : BaseModel
 {
     using (HContext dbContext = new HContext())
     {
         IList <T> entityList = null;
         if (conditions == null)
         {
             entityList = dbContext.Set <T>().ToList();
         }
         else
         {
             entityList = dbContext.Set <T>().Where(conditions).ToList <T>();
         }
         return(entityList);
     }
 }
Пример #4
0
 public bool Delete <T>(T entity) where T : BaseModel
 {
     using (HContext dbContext = new HContext())
     {
         dbContext.Set <T>().Attach(entity);
         dbContext.Entry <T>(entity).State = EntityState.Deleted;
         int result = dbContext.SaveChanges();
         return(result > 0);
     }
 }
Пример #5
0
 public T Add <T>(T entity) where T : BaseModel
 {
     using (HContext dbContext = new HContext())
     {
         dbContext.Set <T>().Attach(entity);
         dbContext.Entry <T>(entity).State = EntityState.Added;
         dbContext.SaveChanges();
         return(entity);
     }
 }
Пример #6
0
 public bool Update <T>(IList <T> entityList) where T : BaseModel
 {
     using (HContext dbContext = new HContext())
     {
         foreach (var entity in entityList)
         {
             dbContext.Set <T>().Attach(entity);
             dbContext.Entry <T>(entity).State = EntityState.Modified;
         }
         int result = dbContext.SaveChanges();
         return(result > 0);
     }
 }
Пример #7
0
 public IList <T> Add <T>(IList <T> entityList) where T : BaseModel
 {
     using (HContext dbContext = new HContext())
     {
         foreach (T entity in entityList)
         {
             dbContext.Set <T>().Attach(entity);
             dbContext.Entry <T>(entity).State = EntityState.Added;
         }
         dbContext.SaveChanges();
         return(entityList);
     }
 }