Open Closed

What are the best practices for BulkUpdates using a custom BulkOperation provider? #9079


User avatar
0
TDonaghe created

I'm using ABP 8.2.2
We want to use a customer bulk operation provider, but we've noticed that updates to our database seem to happen twice in some cases.
In one specific scenario in a Domain manager method, we are modifying a value on a domain object we can call Foo here and then adding a child object to a collection on Foo.
Previously we were making a call to our custom bulk operation library instead of calling UpdateMany on our ABP repository. We noticed the unexpected behavior of two updates happening here.
So I decided to comment out the call to our our bulk operation library and have no update call at all. In this case, the update still happens.
We realized this is probably because of the default behavior of the uow autoSaving. I've made multiple attempts to turn off this autoSave, all without success so far. I've attempted to disable change tracking on both the Foo repository and even on the Domain Manager and still the update happens.

What's the best way to handle this situation? We want the update to happen through our bulk operation library and only there.

Thanks!


1 Answer(s)
  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    Hi,

    In Entity Framework doesn't define any kind of bulk operations and it uses AddRangeAsync() method. If the database provider and EF adapter package supports, it updates database once but it's not guaranteed. It depends on your provider and provider package.

    https://github.com/abpframework/abp/blob/7fb232b7b73d0031e1c40c5a2385a6373c4c2729/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs#L139-L150

    You can provide a method for bulk operations by implementing IEfCoreBulkOperationProvider on your own and use such libraries like [EntityFramework-Plus(https://github.com/zzzprojects/EntityFramework-Plus) inside this provider implementation:

    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(IEfCoreBulkOperationProvider))]
    public class MyProjectNameBulkOperationProvider : IEfCoreBulkOperationProvider, ITransientDependency
    {
        public async Task DeleteManyAsync<TDbContext, TEntity>(IEfCoreRepository<TEntity> repository, IEnumerable<TEntity> entities, bool autoSave, CancellationToken cancellationToken)
             where TDbContext : IEfCoreDbContext where TEntity : class, IEntity
        {
            // Your custom bulk delete logic here...
    
        }
    
        public async Task InsertManyAsync<TDbContext, TEntity>(IEfCoreRepository<TEntity> repository, IEnumerable<TEntity> entities, bool autoSave, CancellationToken cancellationToken)
             where TDbContext : IEfCoreDbContext where TEntity : class, IEntity
        {
            // Your custom bulk insert logic here...
    
        }
    
        public async Task UpdateManyAsync<TDbContext, TEntity>(IEfCoreRepository<TEntity> repository, IEnumerable<TEntity> entities, bool autoSave, CancellationToken cancellationToken)
             where TDbContext : IEfCoreDbContext where TEntity : class, IEntity
        {
            // Your custom bulk update logic here...
    
        }
    }
    
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.3.0-preview. Updated on April 09, 2025, 10:18