.NET Bulk Operations Bulk Update

Description

The BulkUpdate method lets you update a large number of entities in your database.

// easy to use
bulk.DestinationTableName = "Customers";
bulk.BulkUpdate(customers);

// Easy to customize
bulk.DestinationTableName = "Customers";
bulk.BatchSize = 100;
bulk.AutoMapOutputDirection = true;
bulk.BulkUpdate(customers);

Try it (Entity): .NET Core | .NET Framework

Try it (DataTable): .NET Core | .NET Framework

Performance Comparison

Operations 1,000 Entities 2,000 Entities 5,000 Entities
BulkUpdate 80 ms 110 ms 170 ms

Try it (Entity): .NET Core | .NET Framework

Try it (DataTable): .NET Core | .NET Framework

HINT:

A lot of factors might affect the benchmark time such as index, column type, latency, throttling, etc.

Scenarios

The BulkUpdate method is fast but also flexible to let you handle various scenarios such as:

Advantages

  • Easy to use
  • Flexible
  • Increase performance
  • Increase application responsiveness

Getting Started

Bulk Update

The BulkUpdate and BulkUpdateAync methods your let update a large number of entities in your database.

bulk.BulkUpdate(customers);

bulk.BulkUpdateAsync(customers, cancellationToken);

Try it (Entity): .NET Core | .NET Framework

Try it (DataTable): .NET Core | .NET Framework

Bulk Update with options

The options parameter lets you use a lambda expression to customize the way entities are updated.

bulk.AutoMapKeyExpression = c => c.Code;
bulk.BulkUpdate(customers);

Try it (Entity): .NET Core | .NET Framework

bulk.AutoMapKeyName = "Code";
bulk.BulkUpdate(dtCustomers);

Try it (DataTable): .NET Core | .NET Framework

Real-Life Scenarios

Update and include/exclude properties

You want to update your entities but only for specific properties.

  • ColumnInputExpression: This option lets you choose which properties to map.
  • IgnoreOnUpdateExpression: This option lets you ignore properties that are auto-mapped.
bulk.IgnoreOnUpdateExpression = c =>  new { c.ColumnToIgnore };
bulk.BulkUpdate(customers.Skip(2));

Try it (Entity): .NET Core | .NET Framework

var columnMapping = new ColumnMapping("CreatedDate");
                
columnMapping.IgnoreOnUpdate = true;
                    
bulk.ColumnMappings.Add("CustomerID", true);
bulk.ColumnMappings.Add("UpdatedDate");
bulk.ColumnMappings.Add("Name");
bulk.ColumnMappings.Add(columnMapping);
bulk.BulkUpdate(dtCustomers);

Try it (DataTable): .NET Core | .NET Framework

Update with custom key

You want to update entities, but you don't have the primary key. The ColumnPrimaryKeyExpression lets you use as a key any property or combination of properties.

bulk.AutoMapKeyExpression = c => c.Code;
bulk.BulkUpdate(customers);

Try it (Entity): .NET Core | .NET Framework

bulk.AutoMapKeyName = "Code";
bulk.BulkUpdate(customers);

Try it (DataTable): .NET Core | .NET Framework

You want to update entities but also automatically insert related child entities.

bulk.DestinationTableName = "Invoices";
bulk.AutoMapOutputIdentity = true;

bulk.BulkUpdate(invoices);

// SET foreign key value            
invoices.ForEach(x => x.Items.ForEach(y => y.InvoiceID = x.InvoiceID));

bulk.DestinationTableName = "InvoiceItems"; 
bulk.BulkUpdate(invoices.SelectMany(x => x.Items).ToList());

Try it (Entity): .NET Core | .NET Framework

Try it (DataTable): .NET Core | .NET Framework

More scenarios

Hundred of scenarios has been solved and are now supported.

The best way to ask for a special request or to find out if a solution for your scenario already exists is by contacting us: info@zzzprojects.com

Documentation

BulkUpdate

Methods
Name Description Example (DataTable) Example (Entity)
BulkUpdate<T>(items) Bulk update entities in your database. .NET Core / .NET Framework .NET Core / .NET Framework
BulkUpdateAsync<T>(items) Bulk update entities asynchronously in your database.
BulkUpdateAsync<T>(items, cancellationToken) Bulk update entities asynchronously in your database.
Options

More options can be found here: