30% OFF - 9th Anniversary discount on Dapper Plus until December 15 with code: ZZZANNIVERSARY9

Bulk Updating Data With Dapper

Dapper provides the Execute method for updating data. However, a database roundtrip is required for every data you update. The more database roundtrip you do, the longer it will take to complete all your updates.

It is possible to dramatically reduce the number of database roundtrips by using the BulkUpdate method from the Dapper Plus third-party library.

Performance can be improved by up 5000% and more depending on the provider.

Getting Started

NuGet Package: https://www.nuget.org/packages/Z.Dapper.Plus/

Documentation:

Dapper BulkUpdate

Dapper Plus BulkUpdate method allows you to update multiple entities in a single round trip. It is more efficient than updating each entity one by one and can help improve your application's performance by reducing the number of database calls.

  • The BulkUpdate method allows you to specify which properties to update.
  • With the BulkUpdate method, you can easily update thousands of records at once and make sure that your data is always up-to-date.

To perform a BulkUpdate, you need to pass your entities in the parameters:

language-csharp
|
connection.BulkUpdate(customers);

Dapper BulkUpdate with Global Context Mapping

An entity type can be mapped more than once using a mapping key. For example, for a specific scenario, you only want to update some properties:

language-csharp
|
// Global Mapping requires to be mapped globally only once
DapperPlusManager.Entity<Customer>("Customer_CustomMap")
	.Map(nameof(Customer.Email))
	.Map(nameof(Customer.Name));
 
// ...code...
 
connection.BulkUpdate("Customer_CustomMap", customers);

Dapper BulkUpdate with Instance Context Mapping

By using an instance context mapping, you can choose to map your entity type at runtime:

language-csharp
|
public void CustomBulkUpdate<T>(List<T> entities, List<string> propertieNames) where T : class
{
	var context = new DapperPlusContext();
	
	propertieNames.ForEach(x =>
	{
		context.Entity<T>().Map(x);
	});
	
	context.BulkUpdate(entities);
}

Date Modified: 2023-01-08
Author:

Edit this page in GitHub

Got any Dapper Question?