Dapper Plus Bulk Update Discover how to update multiple rows
Description
Dapper Plus provides an extension method called BulkUpdate that is used to update multiple records in a database table at once. It is the fastest and most efficient way to update entities in your database. It can update multiple entities in one database round trip
- The
BulkUpdate
method takes a list of entities as a parameter and updates each entity in the database. - To use the
BulkUpdate
method, you first need to create a list of entities that you want to update. - Then, you can call the
BulkUpdate
method and pass in the list of entities. - The method will update each entity in the database.
It updates entities using Bulk Operation and with BulkUpdate
, you can:
Example - Update Single
You can use the BulkUpdate
method to update a single record. The following example updates a single record into the Customers table.
List<Customer> customers; using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { customers = connection.Query<Customer>("Select Top 1 * FROM CUSTOMERS").ToList(); } customers.ForEach(x => x.CustomerName = "ExampleBulkUpdate"); DapperPlusManager.Entity<Customer>().Table("Customers"); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkUpdate(customers); }
Try it: .NET Core | .NET Framework
Example - Update Many
The ability to update multiple records with a single database call can significantly improve performance. It updates many entities with Bulk Operations. The following example updates a list of customers into the Customers table.
DapperPlusManager.Entity<Customer>().Table("Customers"); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkUpdate(customers); }
Try it: .NET Core | .NET Framework
Example - Update with relation (One to One)
BulkUpdate
allows you to update related entities in the database in one operation. It is especially useful when updating a large number of entities that have relationships with each other.
To use BulkUpdate
when entities have a relationship, simply specify the relationship between the entities when you configure the bulk operation.
The following example shows how to use the BulkUpdate
with a one-to-one relationship between the entities.
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID); DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkUpdate(suppliers, x => x.Product); }
Try it: .NET Core | .NET Framework
Example - Update with relation (One to Many)
Dapper Plus can also allow you to update a list of entities with a one-to-many relationship with Bulk Operation as shown in the following example.
DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID); DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID); using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools())) { connection.BulkUpdate(suppliers, x => x.Products); }
Try it: .NET Core | .NET Framework
Related Articles
ZZZ Projects