From 2c0b11336ce7e9429dbbbba62f209878a3d26769 Mon Sep 17 00:00:00 2001 From: enisn Date: Thu, 17 Dec 2020 13:36:20 +0300 Subject: [PATCH] Update BulkOperations implementation as xxRange methods for EfCoreRepository --- .../EntityFrameworkCore/EfCoreRepository.cs | 114 +++++++++++------- 1 file changed, 70 insertions(+), 44 deletions(-) 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 4866b9daea..437e32ad40 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 @@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; +using Nito.AsyncEx; using System; using System.Collections.Generic; using System.Linq; @@ -62,6 +63,32 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore return savedEntity; } + public override async Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) + { + foreach (var entity in entities) + { + CheckAndSetId(entity); + } + + if (BulkOperationProvider != null) + { + await BulkOperationProvider.InsertManyAsync( + this, + entities, + autoSave, + cancellationToken + ); + return; + } + + await DbSet.AddRangeAsync(entities); + + if (autoSave) + { + await DbContext.SaveChangesAsync(); + } + } + public async override Task UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) { DbContext.Attach(entity); @@ -76,6 +103,28 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore return updatedEntity; } + public override async Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) + { + if (BulkOperationProvider != null) + { + await BulkOperationProvider.UpdateManyAsync( + this, + entities, + autoSave, + cancellationToken + ); + + return; + } + + DbSet.UpdateRange(entities); + + if (autoSave) + { + await DbContext.SaveChangesAsync(); + } + } + public async override Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) { DbSet.Remove(entity); @@ -86,6 +135,27 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore } } + public override async Task DeleteManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) + { + if (BulkOperationProvider != null) + { + await BulkOperationProvider.DeleteManyAsync( + this, + entities, + autoSave, + cancellationToken); + + return; + } + + DbSet.RemoveRange(entities); + + if (autoSave) + { + await DbContext.SaveChangesAsync(); + } + } + public async override Task> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default) { return includeDetails @@ -113,50 +183,6 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore .ToListAsync(GetCancellationToken(cancellationToken)); } - public override Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) - { - if (BulkOperationProvider != null) - { - return BulkOperationProvider.InsertManyAsync( - this, - entities, - autoSave, - cancellationToken - ); - } - - return base.InsertManyAsync(entities, autoSave, cancellationToken); - } - - public override Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) - { - if (BulkOperationProvider != null) - { - return BulkOperationProvider.UpdateManyAsync( - this, - entities, - autoSave, - cancellationToken - ); - } - - return base.UpdateManyAsync(entities, autoSave, cancellationToken); - } - - public override Task DeleteManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) - { - if (BulkOperationProvider != null) - { - return BulkOperationProvider.DeleteManyAsync( - this, - entities, - autoSave, - cancellationToken); - } - - return base.DeleteManyAsync(entities, autoSave, cancellationToken); - } - protected override IQueryable GetQueryable() { return DbSet.AsQueryable();