Example #1
0
        public static DbContext CreateMyBlogEntities()
        {
            DbContext myBlogEntities = (DbContext)CallContext.GetData("MyBlogEntities");

            if (myBlogEntities == null)
            {
                myBlogEntities = new CompilationSystemEntities();
                myBlogEntities.Database.CreateIfNotExists();
                CallContext.SetData("MyBlogEntities", myBlogEntities);
            }
            return(myBlogEntities);
        }
Example #2
0
 /// <summary>
 /// 添加一条数据
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public bool AddEntity(T entity)
 {
     try
     {
         CompilationSystemEntities.Set <T>().Add(entity);
         DBSession.SaveChanges();
         //return entity;
     }
     catch (Exception e)
     {
         return(false);
     }
     return(true);
 }
Example #3
0
        /// <summary>
        /// 查询单页数据
        /// </summary>
        /// <typeparam name="orderEntity">按orderEntity排序</typeparam>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="totalCount">总数据条数</param>
        /// <param name="whereLambda">查询条件</param>
        /// <param name="orderbyLambda">排序条件</param>
        /// <param name="isAsc">是否升序</param>
        /// <returns></returns>
        public IQueryable <T> LoadPageEntities <orderEntity>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression <Func <T, bool> > whereLambda, System.Linq.Expressions.Expression <Func <T, orderEntity> > orderbyLambda, bool isAsc)
        {
            var temp = CompilationSystemEntities.Set <T>().AsNoTracking <T>().Where <T>(whereLambda);

            totalCount = temp.Count();
            if (isAsc)
            {
                temp = temp.OrderBy <T, orderEntity>(orderbyLambda).Skip <T>(pageIndex - 1).Take <T>(pageSize);
            }
            else
            {
                temp = temp.OrderByDescending <T, orderEntity>(orderbyLambda).Skip <T>(pageIndex - 1).Take <T>(pageSize);
            }
            return(temp);
        }
Example #4
0
 /// <summary>
 /// 编辑一条数据
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public bool EditEntity(T entity)
 {
     try
     {
         if (entity == null)
         {
             throw new ArgumentException("entity");
         }
         if (CompilationSystemEntities.Entry(entity).State == EntityState.Detached)
         {
             HandleDetached(entity);
         }
         CompilationSystemEntities.Set <T>().Attach(entity);
         CompilationSystemEntities.Entry <T>(entity).State = System.Data.Entity.EntityState.Modified;
         DBSession.SaveChanges();
     }
     catch (Exception e)
     {
         return(false);
     }
     return(true);
 }
Example #5
0
 /// <summary>
 /// 查询数据
 /// </summary>
 /// <param name="whereLambda">查询条件</param>
 /// <returns></returns>
 public IQueryable <T> LoadEntities(System.Linq.Expressions.Expression <Func <T, bool> > whereLambda)
 {
     return(CompilationSystemEntities.Set <T>().AsNoTracking <T>().Where <T>(whereLambda));
 }