コード例 #1
0
 /// <summary>
 /// 按主键ID单条删除
 /// </summary>
 /// <param name="predicate"></param>
 /// <returns></returns>
 public virtual int Delete(int id)
 {
     using (erp_1807Entities db = new erp_1807Entities())
     {
         var Entity = db.Set <TEntity>().Find(id);
         db.Set <TEntity>().Remove(Entity);
         return(db.SaveChanges());
     }
 }
コード例 #2
0
        /// <summary>
        /// 传递拉姆达表达式删除
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public virtual int Delete(Expression <Func <TEntity, bool> > predicate)
        {
            using (erp_1807Entities db = new erp_1807Entities())
            {
                var mm = db.Set <TEntity>().FirstOrDefault(predicate);

                db.Set <TEntity>().Remove(mm);

                db.Entry <TEntity>(mm).State = System.Data.Entity.EntityState.Deleted;

                db.SaveChanges();

                return(1);
            }
        }
コード例 #3
0
 /// <summary>
 /// 按主键ID批量删除
 /// </summary>
 /// <param name="predicate"></param>
 /// <returns></returns>
 public virtual int Delete(int[] idList)
 {
     using (erp_1807Entities db = new erp_1807Entities())
     {
         return(db.Set <TEntity>().DeleteByKey(idList));
     }
 }
コード例 #4
0
 public virtual IQueryable <TEntity> GetQuery()
 {
     using (erp_1807Entities db = new erp_1807Entities())
     {
         return(db.Set <TEntity>().AsQueryable());
     }
 }
コード例 #5
0
 /// <summary>
 /// 异步方法
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public virtual async Task CreateAsync(TEntity entity)
 {
     using (erp_1807Entities db = new erp_1807Entities())
     {
         await db.Set <TEntity>().SingleInsertAsync(entity);
     }
 }
コード例 #6
0
 /// <summary>
 /// 根据条件查询多条实体
 /// </summary>
 /// <param name="predicate"></param>
 /// <returns></returns>
 public virtual IList <TEntity> GetList(Expression <Func <TEntity, bool> > predicate)
 {
     using (erp_1807Entities db = new erp_1807Entities())
     {
         return(db.Set <TEntity>().Where(predicate).ToList());
     }
 }
コード例 #7
0
 /// <summary>
 /// 获取所有实体
 /// </summary>
 /// <returns></returns>
 public virtual IList <TEntity> GetAll()
 {
     using (erp_1807Entities db = new erp_1807Entities())
     {
         return(db.Set <TEntity>().ToList());
     }
 }
コード例 #8
0
 /// <summary>
 /// 根据条件获取单条实体
 /// </summary>
 /// <param name="predicate"></param>
 /// <returns></returns>
 public virtual TEntity Find(Expression <Func <TEntity, bool> > predicate)
 {
     using (erp_1807Entities db = new erp_1807Entities())
     {
         return(db.Set <TEntity>().FirstOrDefault(predicate));
     }
 }
コード例 #9
0
 /// <summary>
 /// 根据主键查
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public virtual TEntity Find(int id)
 {
     using (erp_1807Entities db = new erp_1807Entities())
     {
         return(db.Set <TEntity>().Find(id));
     }
 }
コード例 #10
0
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual int Create(TEntity entity)
        {
            using (erp_1807Entities db = new erp_1807Entities())
            {
                db.Set <TEntity>().Add(entity);

                return(db.SaveChanges());
            }
        }
コード例 #11
0
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual int Update(Expression <Func <TEntity, bool> > predicate, TEntity entity)
        {
            using (erp_1807Entities db = new erp_1807Entities())
            {
                var Entity = db.Set <TEntity>().FirstOrDefault(predicate);

                if (Entity != null)
                {
                    db.Entry <TEntity>(entity).State = System.Data.Entity.EntityState.Modified;
                    return(db.SaveChanges());
                }
                else
                {
                    return(0);
                }
            }
        }
コード例 #12
0
        /// <summary>
        /// 根据分页查询
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public virtual Tuple <IList <TEntity>, int> GetListByPage(Expression <Func <TEntity, int> > keySelector, int pageIndex = 1, int pageSize = 10, params Expression <Func <TEntity, bool> >[] predicate)
        {
            using (erp_1807Entities db = new erp_1807Entities())
            {
                var list = db.Set <TEntity>().AsQueryable();
                foreach (var item in predicate)
                {
                    var nodeType = item.Body.NodeType;

                    if (nodeType == ExpressionType.Equal)
                    {
                        BinaryExpression binaryExpression = item.Body as BinaryExpression;

                        if (binaryExpression.Right != null)
                        {
                            list = list.Where(item);
                        }
                    }
                    if (nodeType == ExpressionType.Call)
                    {
                        MethodCallExpression methodCallExpression = item.Body as MethodCallExpression;

                        ConstantExpression constantExpression = methodCallExpression.Arguments[0] as ConstantExpression;

                        if (constantExpression.Value != null)
                        {
                            list = list.Where(item);
                        }
                    }
                }

                Tuple <IList <TEntity>, int> tuple = new Tuple <IList <TEntity>, int>
                                                     (
                    item1: list.OrderBy(keySelector).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(),
                    item2: list.Count()
                                                     );

                return(tuple);
            }
        }