In this article, I am going to discuss how to perform BulkUpdate in Entity Framework using Z.EntityFramework.Extensions. Please read our previous article where we discussed how to perform Bulk Insert in Entity Framework.
Note: We are going to work with the same example that we created in our Introduction to Entity Framework Database First Approach Video. Please read our introduction to Entity Framework Database First Video before proceeding to this Video.
Z.EntityFramework.Extensions:
Here, we are going to use Z.EntityFramework.Extensions to perform Bulk Update Operation. So, first, open the NuGet Package Manager window and search for Z.EntityFramework.Extensions package. Select Z.EntityFramework.Extensions and then select the Project and choose the latest version and finally click on the Install button as shown in the below image which will install Z.EntityFramework.Extensions DLL into your project.
Bulk Update in Entity Framework
The BulkUpdate and BulkUpdateAync methods extend our DbContext object which allows us to update a large number of entities into the database. The syntax to use the BulkUpdate extension method as follows:
context.BulkUpdate(studentsList);
context.BulUpdateAsync(studentsList, cancellationToken);
Example: BulkUpdate example using Entity Framework Extensions
In the below example, first, we fetch all the students whose StandardId is 1 and then update the First Name and Last Name and finally update the updated data to the database using the BulkUpdate method.
Output:
Performance Comparison Between SaveChanges and BulkUpdate:
Let see the performance benchmark between the saveChanges and BulkUpdate extension method. In the below example, first, we are inserting 1000 students into the student table and then we are going updating the first name and last name of all the students using both the approach. Here, will also print the time taken to complete the execution of both approaches.
Output: You can see in the below output, the SaveChanges method took 954 ms time to update 1005 rows into the database while the BulkUpdate extension method took only 147 ms to update the same 1005 records into the database.
Note: A lot of factors need to be considered for affecting the benchmark time such as index, column type, latency, throttling, etc.
Why BulkUpdate is faster than SaveChanges?
In real-time applications, it is a common task to update 1000 entities. The SaveChanges method makes it quite impossible to handle this kind of situation due to the number of database round-trips required. The SaveChanges perform one database round-trip for every entity to update. So, if you need to update 10,000 entities, 10,000 database round-trips will be performed which is make it slow. If you are performing the update operation using any API, then it might be possible for the client who consumes your API gets a time-out error as the update operation going to take a huge amount of time.
The BulkUpdate in counterpart requires the minimum database round-trips possible. For example, under the hood for SQL Server, a SqlBulkCopy is performed first in a temporary table, then an UPDATE from the temporary table to the destination table is performed which is the fastest way available.
Advantages of BulkUpdate Extension Method in Entity Framework:
- Easy to use, simply calling the BulkUpdate Method
- Flexible as it also provides different options.
- Increase performance due to reducing database round-trips
- Increase application responsiveness
- Due to reduction in database round-trips, it also reduces the load on the database
Scenarios to use BulkUpdate
The BulkUpdate Extension method is not only fast but also so flexible that lets us handle various scenarios in Entity Framework. Some of them are as follows:
- Update and include/exclude properties
- Update with custom key
- Update with related child entities (Include Graph)
- Update with future action