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 ab4cd0049f..c4c885f56e 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 @@ -95,7 +95,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); @@ -111,9 +111,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); @@ -141,7 +146,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(); @@ -157,15 +162,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) ); @@ -175,7 +186,7 @@ public class EfCoreRepository : RepositoryBase, IE var dbContext = await GetDbContextAsync(); - dbContext.Set().UpdateRange(entities); + dbContext.Set().UpdateRange(entityArray); if (autoSave) { @@ -183,7 +194,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(); @@ -195,15 +206,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 ); @@ -213,7 +230,7 @@ public class EfCoreRepository : RepositoryBase, IE var dbContext = await GetDbContextAsync(); - dbContext.RemoveRange(entities); + dbContext.RemoveRange(entityArray.Select(x => x)); if (autoSave) { @@ -221,26 +238,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, @@ -263,17 +280,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) @@ -287,7 +304,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(); @@ -339,7 +356,7 @@ public class EfCoreRepository : RepositoryBase, IE return AbpEntityOptions.DefaultWithDetailsFunc(GetQueryable()); } - public override async Task> WithDetailsAsync() + public async override Task> WithDetailsAsync() { if (AbpEntityOptions.DefaultWithDetailsFunc == null) { @@ -358,7 +375,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 e128c8dee8..23e613fc8f 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 @@ -137,9 +137,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) { @@ -226,6 +230,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) { @@ -356,12 +364,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)) { @@ -381,7 +395,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; }