Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support bulk operations extension methods on DbSet instead of just DbContext #361

Closed
gandhis1 opened this issue Jul 13, 2020 · 7 comments
Closed
Labels

Comments

@gandhis1
Copy link
Contributor

The standard EF Core AddRange() and RemoveRange() methods are available on either the DbSet or DbContext classes. Is there a compelling reason to not offer both interfaces for bulk operations as well?

@borisdj
Copy link
Owner

borisdj commented Jul 14, 2020

Not particularly.
Methods naming are Insert, Read, Update, Delete so CRUD convention, except Create->Insert.

@borisdj borisdj closed this as completed Jul 14, 2020
@gandhis1
Copy link
Contributor Author

gandhis1 commented Jul 14, 2020

Sorry, I don't really mean the naming of the methods - more that because BulkInsert() is available on DbContext it should be available on DbSet as well. This makes the bulk operations consistent with the standard EF operations which can be invoked on either type.

I am already able to do:

dbContext.BulkInsert(entities);
dbContext.BulkDelete(entities);

But I would like to also be able to do:

dbContext.SomeTable.BulkInsert(entities);
dbContext.SomeTable.BulkDelete(entities);

Would you accept a PR to add this interface?

@cwilby
Copy link

cwilby commented May 23, 2021

Looking into this. I'd also like to be able to access Bulk* methods from a DbSet, rather than via DbContext.

This is because we split our DataContext collections across repositories, where each Repository class has a DbSet that it performs operations (including bulk operations) on.

@cwilby
Copy link

cwilby commented May 23, 2021

Doesn't look promising. The static methods exposed by DbContextBulkExtensions.cs use a DbContextBulkTransaction which depends on DbContext to function.

I had a go at adding a new DbSetBulkExtensions class, you'd have to just pass in the context anyway making it redundant/pointless to implement.

@gandhis1
Copy link
Contributor Author

Does anything here work? https://stackoverflow.com/a/50864373

@borisdj
Copy link
Owner

borisdj commented May 24, 2021

Library already has it's method for getting DbContext.
So implementation could be like this (example for one method):

public static class IQueryableBulkExtensions
{
    public static void BulkInsert<T>(this IQueryable query, IList<T> entities, BulkConfig bulkConfig = null, Action<decimal> progress = null, Type type = null) where T : class
    {
        var context = BatchUtil.GetDbContext(query);
        context.BulkInsert(entities, bulkConfig, progress, type);
    }
    
    / ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants