diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs index 96d7b4111e..03af8d1fb4 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs @@ -107,7 +107,7 @@ public class EfCoreRepository : RepositoryBase, IE ); } - public override async Task InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) + public async override Task InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) { CheckAndSetId(entity); @@ -123,9 +123,14 @@ public class EfCoreRepository : RepositoryBase, IE return savedEntity; } - public override async Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) + public async override Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) { var entityArray = entities.ToArray(); + if (entityArray.IsNullOrEmpty()) + { + return; + } + var dbContext = await GetDbContextAsync(); cancellationToken = GetCancellationToken(cancellationToken); @@ -153,7 +158,7 @@ public class EfCoreRepository : RepositoryBase, IE } } - public override async Task UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) + public async override Task UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) { var dbContext = await GetDbContextAsync(); @@ -169,15 +174,21 @@ public class EfCoreRepository : RepositoryBase, IE return updatedEntity; } - public override async Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) + public async override Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) { + var entityArray = entities.ToArray(); + if (entityArray.IsNullOrEmpty()) + { + return; + } + cancellationToken = GetCancellationToken(cancellationToken); if (BulkOperationProvider != null) { await BulkOperationProvider.UpdateManyAsync( this, - entities, + entityArray, autoSave, GetCancellationToken(cancellationToken) ); @@ -187,7 +198,7 @@ public class EfCoreRepository : RepositoryBase, IE var dbContext = await GetDbContextAsync(); - dbContext.Set().UpdateRange(entities); + dbContext.Set().UpdateRange(entityArray); if (autoSave) { @@ -195,7 +206,7 @@ public class EfCoreRepository : RepositoryBase, IE } } - public override async Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) + public async override Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) { var dbContext = await GetDbContextAsync(); @@ -207,15 +218,21 @@ public class EfCoreRepository : RepositoryBase, IE } } - public override async Task DeleteManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) + public async override Task DeleteManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) { + var entityArray = entities.ToArray(); + if (entityArray.IsNullOrEmpty()) + { + return; + } + cancellationToken = GetCancellationToken(cancellationToken); if (BulkOperationProvider != null) { await BulkOperationProvider.DeleteManyAsync( this, - entities, + entityArray, autoSave, cancellationToken ); @@ -225,7 +242,7 @@ public class EfCoreRepository : RepositoryBase, IE var dbContext = await GetDbContextAsync(); - dbContext.RemoveRange(entities); + dbContext.RemoveRange(entityArray.Select(x => x)); if (autoSave) { @@ -233,26 +250,26 @@ public class EfCoreRepository : RepositoryBase, IE } } - public override async Task> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default) + public async override Task> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default) { return includeDetails ? await (await WithDetailsAsync()).ToListAsync(GetCancellationToken(cancellationToken)) : await (await GetDbSetAsync()).ToListAsync(GetCancellationToken(cancellationToken)); } - public override async Task> GetListAsync(Expression> predicate, bool includeDetails = false, CancellationToken cancellationToken = default) + public async override Task> GetListAsync(Expression> predicate, bool includeDetails = false, CancellationToken cancellationToken = default) { return includeDetails ? await (await WithDetailsAsync()).Where(predicate).ToListAsync(GetCancellationToken(cancellationToken)) : await (await GetDbSetAsync()).Where(predicate).ToListAsync(GetCancellationToken(cancellationToken)); } - public override async Task GetCountAsync(CancellationToken cancellationToken = default) + public async override Task GetCountAsync(CancellationToken cancellationToken = default) { return await (await GetDbSetAsync()).LongCountAsync(GetCancellationToken(cancellationToken)); } - public override async Task> GetPagedListAsync( + public async override Task> GetPagedListAsync( int skipCount, int maxResultCount, string sorting, @@ -275,17 +292,17 @@ public class EfCoreRepository : RepositoryBase, IE return DbSet.AsQueryable(); } - public override async Task> GetQueryableAsync() + public async override Task> GetQueryableAsync() { return (await GetDbSetAsync()).AsQueryable(); } - protected override async Task SaveChangesAsync(CancellationToken cancellationToken) + protected async override Task SaveChangesAsync(CancellationToken cancellationToken) { await (await GetDbContextAsync()).SaveChangesAsync(cancellationToken); } - public override async Task FindAsync( + public async override Task FindAsync( Expression> predicate, bool includeDetails = true, CancellationToken cancellationToken = default) @@ -299,7 +316,7 @@ public class EfCoreRepository : RepositoryBase, IE .SingleOrDefaultAsync(GetCancellationToken(cancellationToken)); } - public override async Task DeleteAsync(Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default) + public async override Task DeleteAsync(Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default) { var dbContext = await GetDbContextAsync(); var dbSet = dbContext.Set(); @@ -358,7 +375,7 @@ public class EfCoreRepository : RepositoryBase, IE return AbpEntityOptions.DefaultWithDetailsFunc(GetQueryable()); } - public override async Task> WithDetailsAsync() + public async override Task> WithDetailsAsync() { if (AbpEntityOptions.DefaultWithDetailsFunc == null) { @@ -377,7 +394,7 @@ public class EfCoreRepository : RepositoryBase, IE ); } - public override async Task> WithDetailsAsync(params Expression>[] propertySelectors) + public async override Task> WithDetailsAsync(params Expression>[] propertySelectors) { return IncludeDetails( await GetQueryableAsync(), diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs index 58f5c72716..4ebe61f64e 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs @@ -138,9 +138,13 @@ public class MongoDbRepository public async override Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) { - cancellationToken = GetCancellationToken(cancellationToken); - var entityArray = entities.ToArray(); + if (entityArray.IsNullOrEmpty()) + { + return; + } + + cancellationToken = GetCancellationToken(cancellationToken); foreach (var entity in entityArray) { @@ -228,6 +232,10 @@ public class MongoDbRepository public async override Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) { var entityArray = entities.ToArray(); + if (entityArray.IsNullOrEmpty()) + { + return; + } foreach (var entity in entityArray) { @@ -358,12 +366,18 @@ public class MongoDbRepository bool autoSave = false, CancellationToken cancellationToken = default) { + var entityArray = entities.ToArray(); + if (entityArray.IsNullOrEmpty()) + { + return; + } + cancellationToken = GetCancellationToken(cancellationToken); var softDeletedEntities = new Dictionary(); var hardDeletedEntities = new List(); - foreach (var entity in entities) + foreach (var entity in entityArray) { if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)) && !IsHardDeleted(entity)) { @@ -383,7 +397,7 @@ public class MongoDbRepository if (BulkOperationProvider != null) { - await BulkOperationProvider.DeleteManyAsync(this, entities, dbContext.SessionHandle, autoSave, cancellationToken); + await BulkOperationProvider.DeleteManyAsync(this, entityArray, dbContext.SessionHandle, autoSave, cancellationToken); return; }