In this tutorial, we are going to cover Entity Framework Core (EF Core) bulk insert, update and delete options using Asp.Net Core 3.0 in Visual Studio 2019. We will use entity framework core Bulk extension to insert, update and delete multiple records. There are some other solutions to insert, update and delete multiple records in the fastest way, but using Bulk extension is the most significant way to insert, update, and delete multiple records.

So, first-of-all, we will create a new project using Asp.Net Core 3.0 and Visual studio 2019 and then we will add a new Model class and then create a new database. Then we will install the entity framework core bulk extension and then we will see how to use bulk extension to insert, update and delete multiple records.

Entity Framework Core 3.0 Bulk insert, update and delete using Asp.Net Core 3.0

Let’s see how to use bulk insert, update and delete using entity framework core 3.0 in step by step.

Step # 1: Create a new project using Visual Studio 2019

So, first-of-all, in this step we will create a new Asp.Net Core 3.0 project using visual studio 2019. So, open visual studio 2019 and then “Create a new project” and then choose “ASP.NET Core Web Application” and then enter project name and then click on the “Create” button. Then choose template “Web Application (Model-View-Controller)” and then change authentication from “No Authentication” to “Individual User Account” and then click on the “Create” button.

Step # 2: Create a new Model class

Now, in this step, we will create a new model class and then create a new database. So, right click on the “Models” folder and then add new class with the name of “Employee” as you do see in the below file.

Now, go to ApplicationDbContext class and then enter the DbSet as you do see below in the file line # 17.

Now, go to project manager console and then run these below two commands.

=> add-migration

=> update-database

Step # 3: Install Entity Framework Core Bulk extension

Now, in this step, we will install entity framework core Bulk extension. So, go to nuget package manager and then install the EFCore.BulkExtensions as you do see below in the screenshot.

Step # 4: How to use Bulk extension in Entity Framework Core 3.0 using Asp.Net Core 3.0

In this step, we will see how to use Bulk extension to insert, update and delete multiple records in entity framework core 3.0.

How to insert multiple records using linq entity framework core bulk insert.

Now, in this step, we will see linq entity framework insert multiple records using BulkInsert() option. So, go to Controllers folder and then Home Controller and then add the code as you do see in the below file.

Let’s understand the above code.

Line # 27: In this line, we are calling the Insert Record function. And this InsertRecord() function will return the list of employees with one hundred thousand rows and then assign to employee list.

Line # 28: Now, in this line we are using BulkInsert() to insert the many records at a time. This is the same as the SaveChanges() function. But it is more significant and fast than SaveChanges() function.

Line # 30 to 43: this block of code inserting many records into a list and then returning a list.

Now, run your application and this Index() function will be called automatically. So, once the application compiled successfully, then go to the database and then you will see the one hundred thousand employees’ record is inserted in the employee table.

Execution time difference between BulkInsert() and SaveChanges()

The execution time of one hundred thousand records using BulkInsert() function is 11250 milliseconds and the execution time of the same record using SaveChanges() is 331675 milliseconds.

How to update multiple records using entity framework core bulk update

Now, in this step, we will see how to insert multiple records using entity framework core bulk update option. So, write the code as you do see in the below file.

Let’s understand the above code.

Line # 30: In this line, we are calling the UpdateRecord() function. And this UpdateRecord() function will return the list of employees with their ids and then assign to employee list.

Line # 31: Now, in this line we are using BulkUpdate() to update the many reocrds at a time.

Line # 35 to 51: this block of code will return the list of employees with their ids.

Execution time difference between BulkUpdate() and SaveChanges()

The execution time of one hundred thousand records using BulkUpdate() function is 11239 milliseconds and the execution time of the same record using SaveChanges() is 347288 milliseconds.

How to delete multiple records using entity framework core bulk delete

Now, in this step, we will see how to delete multiple records using entity framework core bulk delete option. So, write the code as you do see in the below file.

Let’s understand the above file code.

Line # 27: In this line, we are getting first 50000 records from employee table and then assigning to employee list.

Line # 28: this line of code will delete the record using BulkDelete().

Execution time difference between BulkDelete() and SaveChanges()

The execution time of one fifty thousand record using BulkDelete() is 17293 milliseconds and the execution time of the same record using SaveChanges() is 151501 milliseconds.

Thank you for reading. Please keep visiting and sharing within your community!

Note:- If you found this blog helpful then: Buy me a coffee If you found this blog helpful then: