Esempio n. 1
0
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="ent"></param>
        /// <param name="isSave">是否保存到数据库,如果为false,则没有保存数据库,必须执行SaveChanges()
        /// 来实现事务</param>
        /// <returns></returns>
        public int Delete(TEntity ent, bool isSave = true)
        {
            ExceptionExtension.IsNull(ent, "ent");

            _entites.Remove(ent);
            return(isSave?_db.SaveChanges():0);
        }
Esempio n. 2
0
        /// <summary>
        /// 批量删除(条件)
        /// </summary>
        /// <param name="where"></param>
        /// <param name="isSave">是否保存到数据库,如果为false,则没有保存数据库,必须执行SaveChanges()
        /// 来实现事务</param>
        /// <returns></returns>
        public int Delete(System.Linq.Expressions.Expression <Func <TEntity, bool> > where, bool isSave = true)
        {
            ExceptionExtension.IsNull(where, "where");

            var ents = _entites.Where(where).ToList();

            return(isSave?_db.SaveChanges():0);
        }
Esempio n. 3
0
        /// <summary>
        /// 批量更新条件相同的实体
        /// </summary>
        /// <param name="where">需要更新的实体条件</param>
        /// <param name="isSave">是否保存到数据库,如果为false,则没有保存数据库,必须执行SaveChanges()
        /// 来实现事务</param>
        /// <returns></returns>
        public int Update(System.Linq.Expressions.Expression <Func <TEntity, bool> > where,
                          bool isSave = true)
        {
            ExceptionExtension.IsNull(where, "where");

            var ents = _entites.Where(where).ToList();

            return(Update(ents, isSave));
        }
Esempio n. 4
0
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ents"></param>
        /// <param name="isSave">是否保存到数据库,如果为false,则没有保存数据库,必须执行SaveChanges()
        /// 来实现事务</param>
        /// <returns></returns>
        public int Delete(IEnumerable <TEntity> ents, bool isSave = true)
        {
            ExceptionExtension.IsNull(ents, "ents");

            foreach (var ent in ents)
            {
                _entites.Remove(ent);
            }
            return(isSave?_db.SaveChanges():0);
        }
Esempio n. 5
0
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="ent"></param>
        /// <param name="isSave">是否保存到数据库,如果为false,则没有保存数据库,必须执行SaveChanges()
        /// 来实现事务</param>
        /// <returns></returns>
        public int Update(TEntity ent, bool isSave = true)
        {
            ExceptionExtension.IsNull(ent, "ent");

            var state = _db.Entry <TEntity>(ent).State;

            if (state == EntityState.Detached)
            {
                _entites.Attach(ent);
            }

            _db.Entry <TEntity>(ent).State = EntityState.Modified;

            return(isSave == true?_db.SaveChanges() : 0);
        }
Esempio n. 6
0
        /// <summary>
        /// 批量更新多个实体
        /// </summary>
        /// <param name="ents"></param>
        /// <param name="isSave">是否保存到数据库,如果为false,则没有保存数据库,必须执行SaveChanges()
        /// 来实现事务</param>
        /// <returns></returns>
        public int Update(IEnumerable <TEntity> ents, bool isSave = true)
        {
            ExceptionExtension.IsNull(ents, "ents");

            foreach (var ent in ents)
            {
                var state = _db.Entry <TEntity>(ent).State;
                if (state == EntityState.Detached)
                {
                    _entites.Attach(ent);
                }

                _db.Entry <TEntity>(ent).State = EntityState.Modified;
            }

            return(isSave == true?_db.SaveChanges() : 0);
        }