Skip to content

EfCore.BulkOperations simplifies bulk operations like insert, update, and delete with efficient SQL queries compatible with most databases.

License

Notifications You must be signed in to change notification settings

hongjs/EfCore.BulkOperations

Repository files navigation

EfCore.BulkOperations

EfCore.BulkOperations simplifies bulk operations like insert, update, and delete with efficient SQL queries compatible with most databases.

EfCore.BulkOperations Mapping columns from unique keys. You can configure custom column mapping if needed.

ps. BulkMerge works with MySQL only.

Go to NuGet


Quality Gate Status Coverage Reliability Rating Security Rating

Example

Bulk Insert

var items = new List<Product> { new Product("Product1", 100m) };
await _dbContext.BulkInsertAsync(items);
var items = new List<Product> { new Product("Product1", 100m) };
await _dbContext.BulkInsertAsync(
    items, 
    option =>
    {
        option.BatchSize = 1000;
        option.CommandTimeout = 120;
        option.IgnoreOnInsert = x => new { x.CreatedAt };
    }
);

Bulk Update

var items = new List<Product> { new Product("Product1", 100m) };
await _dbContext.BulkUpdateAsync(items);
var items = new List<Product> { new Product("Product1", 100m) };
await _dbContext.BulkUpdateAsync(
    items, 
    option => { option.IgnoreOnUpdate = x => new { x.CreatedAt }; }
);
var items = new List<Product> { new Product("Product1", 100m) };
await _dbContext.BulkUpdateAsync(
    items, 
    option => { option.UniqueKeys = x => new { x.Id }; }
);

Bulk Delete

var items = new List<Product> { new Product("Product1", 100m) };
await _dbContext.BulkDeleteAsync(items);
var items = new List<Product> { new Product("Product1", 100m) };
await _dbContext.BulkDeleteAsync(
    items, 
    option => { option.UniqueKeys = x => new { x.Id }; }
);

Bulk Merge (MySql only)

Do not use BulkMergeAsync with other databases; it relies on a MySQL-specific query.

var items = new List<Product> { new Product("Product1", 100m) };
await _dbContext.BulkMergeAsync(items);
await _dbContext.BulkMergeAsync(
    items,
    option =>
    {
        option.IgnoreOnInsert = x => new { x.CreatedAt };
        option.IgnoreOnUpdate = x => new { x.CreatedAt };
    });

Working with Global Transaction

EfCore.BulkOperations utilizes local transactions within bulk processes. If you require manual transaction control, you can pass an existing transaction into the bulk process.

IDbContextTransaction? transaction = null;
DbConnection? connection = null;
try
{
    connection = dbContext.Database.GetDbConnection();
    if (connection.State != ConnectionState.Open) await connection.OpenAsync();
    transaction = await dbContext.Database.BeginTransactionAsync();
    var dbTransaction = transaction.GetDbTransaction();

    await dbContext.Products.AddAsync (item1);
    await dbContext.SaveChangesAsync();
    await dbContext.BulkInsertAsync(list2, null, dbTransaction);
    await dbContext.BulkInsertAsync(list3, null, dbTransaction);

    throw new DbUpdateException("Internal Server Error");
}
catch (Exception)
{
    if (transaction is not null) await transaction.RollbackAsync();
    throw;
}
finally
{
    if (connection is { State: ConnectionState.Open }) await connection.CloseAsync();
}

About

EfCore.BulkOperations simplifies bulk operations like insert, update, and delete with efficient SQL queries compatible with most databases.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages