Exemplo n.º 1
0
        /// <summary>
        /// 批量删除操作
        /// 使用Dapper执行批量操作
        /// </summary>
        /// <typeparam name="TEntity">要执行实体类型</typeparam>
        /// <typeparam name="TKey">实体主键类型</typeparam>
        /// <param name="repository">仓储信息</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="entities">实体数据集合</param>
        /// <returns>影响的行数</returns>
        public static Task <int> BulkDeleteAsync <TEntity, TKey>(this IRepository <TEntity, TKey> repository, IEnumerable <TEntity> entities, DbTransaction transaction = null)
            where TEntity : EntityBase <TKey>
        {
            return(Task.Run(() =>
            {
                var type = typeof(TEntity);
                var property = typeof(TEntity).GetProperties().FirstOrDefault(u => u.Name == "Id");
                if (property == null)
                {
                    throw new ArgumentNullException($"对象 {type} 没有Id属性");
                }
                var sql = SqlGenerateFactory.CreateDeleteSql(type);

                var conn = repository.Connection;
                var command = conn.CreateCommand();
                command.CommandText = sql;
                if (transaction != null)
                {
                    command.Transaction = transaction;
                }
                var count = 0;
                foreach (var entity in entities)
                {
                    var parameter = command.CreateParameter();
                    parameter.ParameterName = property.Name;
                    parameter.DbType = typeMap[property.PropertyType];
                    parameter.Value = property.GetValue(entity);

                    command.Parameters.Add(parameter);
                    count += command.ExecuteNonQuery();
                    command.Parameters.Clear();
                }
                return count;
            }));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 批量插入操作 异步
        /// 使用Dapper执行批量操作
        /// </summary>
        /// <typeparam name="TEntity">要执行实体类型</typeparam>
        /// <typeparam name="TKey">实体主键类型</typeparam>
        /// <param name="repository">仓储信息</param>
        /// <param name="entities">实体数据集合</param>
        /// <returns>影响的行数</returns>
        public static Task <int> BulkInsertAsync <TEntity, TKey>(this IRepository <TEntity, TKey> repository, IEnumerable <TEntity> entities, DbTransaction transaction = null)
            where TEntity : EntityBase <TKey>
        {
            var sql = SqlGenerateFactory.CreateInsertSql(typeof(TEntity));

            return(repository.Connection.ExecuteAsync(sql, entities, transaction));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 批量更新操作
        /// 使用Dapper执行批量操作
        /// </summary>
        /// <typeparam name="TEntity">要执行实体类型</typeparam>
        /// <typeparam name="TKey">实体主键类型</typeparam>
        /// <param name="repository">仓储信息</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="entities">实体数据集合</param>
        /// <returns>影响的行数</returns>
        public static int BulkUpdate <TEntity, TKey>(this IRepository <TEntity, TKey> repository, IEnumerable <TEntity> entities, DbTransaction transaction = null)
            where TEntity : EntityBase <TKey>
        {
            var sql = SqlGenerateFactory.CreateUpdateSql(typeof(TEntity));

            return(repository.Connection.Execute(sql, entities, transaction));
        }