public ResultInfo <object> DeleteEntitysAsTran <TEntity, TKey>(IEnumerable <TEntity> entityList, TKey keyPropteryName) where TEntity : class, new()
        {
            ResultInfo <object> result = new ResultInfo <object>();

            try
            {
                var keyPName = keyPropteryName.ToString();
                var set      = _context.Set <TEntity>();

                entityList.ToList().ForEach(entity =>
                {
                    var tmp = set.AsEnumerable().Where(x => x.GetType().GetProperty(keyPName).GetValue(x, null).ToString() == entity.GetType().GetProperty(keyPName).GetValue(entity, null).ToString()).FirstOrDefault();
                    if (tmp == null)
                    {
                        throw new Exception("未找到要删除的对象!");
                    }
                    set.Remove(tmp);
                });
            }
            catch (Exception ex)
            {
                result.BindAllException(ex);
                result.Success = false;
            }
            return(result);
        }
Beispiel #2
0
        public ResultInfo <object> UpdateEntitys <TEntity, TKey>(IEnumerable <TEntity> entitysList, TKey key) where TEntity : class, new()
        {
            ResultInfo <object> result = new ResultInfo <object>();

            try
            {
                List <TEntity> entitys = new List <TEntity>();
                entitysList.ToList().ForEach(x =>
                {
                    var key1 = key.ToString();
                    var tmp  = _context.Set <TEntity>().AsEnumerable().Where(y => y.GetType().GetProperty(key1).GetValue(y, null) == x.GetType().GetProperty(key1).GetValue(x, null)).FirstOrDefault();
                    if (tmp != null)
                    {
                        entitys.Add(x.Apply_DA(tmp, true));
                    }
                });
                if (entitys.Count < entitysList.Count())
                {
                    throw new Exception("更新的集合未能全部找到!");
                }
                else
                {
                    result = this.UpdateEntitys();
                }
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.Message;
                result.BindAllException(ex);
            }

            return(result);
        }
        public virtual ResultInfo DBExecuteNonQuery(string sql, IEnumerable <OracleParameter> parmCollection = null)
        {
            ResultInfo result = new ResultInfo();

            try
            {
                if (_transation != null)
                {
                    throw new Exception("该方法不适合执行挂起事务的操作!");
                }
                this.Open();
                _cmd             = _connection.CreateCommand();
                _cmd.CommandText = sql;
                if (parmCollection != null)
                {
                    parmCollection.ToList().ForEach(x =>
                    {
                        _cmd.Parameters.Add(x);
                    });
                    //_cmd.Parameters.AddRange(parmCollection.ToArray());
                }
                int count = _cmd.ExecuteNonQuery();
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.BindAllException(ex);
            }
            finally
            {
                this.Close();
            }
            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// 批量更新(新方法)
        /// </summary>
        /// <typeparam name="TEntity">实体类型</typeparam>
        /// <param name="entitysList">实体集合</param>
        /// <param name="csdl">Edmx文件模型名称</param>
        /// <returns></returns>
        public ResultInfo <object> UpdateEntitys_New <TEntity>(IEnumerable <TEntity> entitysList, string csdl = "Model") where TEntity : class, new()
        {
            ResultInfo <object> result = new ResultInfo <object>();

            try
            {
                List <TEntity> entitys = new List <TEntity>();
                entitysList.ToList().ForEach(x =>
                {
                    //var parms = this.Get_EntityKeyMembers(x, csdl).Select(z => z.Value).ToArray();
                    var json = new System.Text.StringBuilder("");
                    this.Get_EntityKeyMembers(x, csdl).ToList().ForEach(y =>
                    {
                        var v = y.Value;
                        json.AppendFormat("{0} : {1} ({2})", y.Key, y.Value, y.ValueType.FullName);
                    });
                    var tmp = _context.Entry(x);//var tmp = this._context.Set<TEntity>().Find(parms);
                    if (tmp.IsNull_DA())
                    {
                        throw new Exception(string.Format("未找到对象:({0}),可能已经被删除!", json.ToString()));
                    }
                    tmp.State = EntityState.Modified;
                });
                result = this.UpdateEntitys();
            }
            catch (System.InvalidOperationException ex)
            {
                result.Success = false;
                result.Message = "请保证使用AsNoTracking()查询,保证返回的实体不被缓存;" + ex.Message;
                result.BindAllException(ex);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.Message;
                result.BindAllException(ex);
            }
            return(result);
        }
        public ResultInfo <object> CommitTransaction()
        {
            ResultInfo <object> result = new ResultInfo <object>();

            try
            {
                var count = _context.SaveChanges();
                result.Success = (count > 0);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.BindAllException(ex);
            }
            return(result);
        }
Beispiel #6
0
        ///// <summary>
        ///// 更新从context中查询出来实体,并返回影响的行数(Data属性)
        ///// </summary>
        ///// <returns></returns>
        public ResultInfo <object> UpdateEntitys()
        {
            ResultInfo <object> result = new ResultInfo <object>();

            try
            {
                int count = _context.SaveChanges();
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Success = false;
                result.BindAllException(ex);
            }
            return(result);
        }
        public ResultInfo <object> DeleteEntitysAsTran <TEntity>(IEnumerable <TEntity> entityList) where TEntity : class, new()
        {
            ResultInfo <object> result = new ResultInfo <object>();

            try
            {
                var set = _context.Set <TEntity>();
                entityList.ToList().ForEach(entity =>
                {
                    set.Remove(entity);
                });
            }
            catch (Exception ex)
            {
                result.BindAllException(ex);
                result.Success = false;
            }
            return(result);
        }
        public ResultInfo <object> AddEntityAsTran <TEntity>(TEntity entity) where TEntity : class, new()
        {
            ResultInfo <object> result = new ResultInfo <object>();

            try
            {
                this.AddEntitysAsTran(new List <TEntity>()
                {
                    entity
                });
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.Message;
                result.BindAllException(ex);
            }
            return(result);
        }
Beispiel #9
0
        /// <summary>
        /// 更新单个对象
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="entity"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public ResultInfo <object> UpdateEntity <TEntity, TKey>(TEntity entity, TKey key) where TEntity : class, new()
        {
            ResultInfo <object> result = new ResultInfo <object>();

            try
            {
                var list = new List <TEntity>();
                list.Add(entity);
                result = UpdateEntitys(list, key);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.Message;
                result.BindAllException(ex);
            }

            return(result);
        }
Beispiel #10
0
        /// <summary>
        /// 批量删除(新方法)
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entityList"></param>
        /// <param name="csdl"></param>
        /// <returns></returns>
        public ResultInfo <object> DeleteEntitys_New <TEntity>(IEnumerable <TEntity> entityList, string csdl = "Model") where TEntity : class, new()
        {
            ResultInfo <object> result = new ResultInfo <object>();

            try
            {
                entityList.ToList().ForEach(x => {
                    var entity   = _context.Entry(x);
                    entity.State = EntityState.Deleted;
                });
                int count = _context.SaveChanges();
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.BindAllException(ex);
                result.Success = false;
            }
            return(result);
        }
Beispiel #11
0
        /// <summary>
        /// 事务执行参数化SQl语句(执行完必须提交或者回滚)(插入、更新和删除)
        /// </summary>
        /// <param name="sql">执行的sql语句</param>
        /// <param name="parmCollection">执行的参数,如果无参可为空</param>
        /// <param name="successAutoCommit">成功时自动提交事务(默认false,表示可继续使用当前开启的事务,使用完需要手动调用CommitTransaction方法)</param>
        /// <returns></returns>
        public virtual ResultInfo <object> DBExecuteNonQueryAsTran(string sql, IEnumerable <DbParameter> parmCollection = null, CommandType cmdType = CommandType.Text, bool successAutoCommit = false)
        {
            ResultInfo <object> result = new ResultInfo <object>();

            try
            {
                if (_transation == null)
                {
                    throw new Exception("该方法需要先开始事务,请执行BeginTransaction相关的方法!");
                }
                this.Open();
                _cmd             = _connection.CreateCommand();
                _cmd.CommandText = sql;
                _cmd.CommandType = cmdType;
                if (parmCollection != null)
                {
                    parmCollection.ToList().ForEach(x => {
                        _cmd.Parameters.Add(x);
                    });
                    //_cmd.Parameters.AddRange(parmCollection.ToArray());
                }
                _cmd.Transaction = this._transation;
                int count = _cmd.ExecuteNonQuery();
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.BindAllException(ex);
            }
            finally
            {
                if (result.Success && successAutoCommit)
                {
                    this.CommitTransaction();
                }
            }
            return(result);
        }
Beispiel #12
0
        /// <summary>
        /// 增加多个实体
        /// </summary>
        /// <typeparam name="TEntity">实体的类型</typeparam>
        /// <param name="entityList">要增加的实体集合</param>
        /// <returns></returns>
        public ResultInfo <object> AddEntitys <TEntity>(IEnumerable <TEntity> entityList)
            where TEntity : class, new()
        {
            ResultInfo <object> result = new ResultInfo <object>();

            try
            {
                var set = _context.Set <TEntity>();
                entityList.ToList().ForEach(entity =>
                {
                    set.Add(entity);
                });
                var count = _context.SaveChanges();
                result.Success = (count > 0);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.BindAllException(ex);
            }
            return(result);
        }