Skip to content

How to Insert, Update and Delete Bulk Data in ASP.NET Core

Problem definition: We need to insert, update and delete bulk data in a SQL Server database in an ASP.NET Core application. For example, to insert 50 employee records into the database in one go, without looping through the employee list, we can use BulkInsert from the EFCore.BulkExtensions NuGet package.

Below is the step-by-step process, explained in detail, so that you can insert, update and delete bulk data in the database using BulkInsert, BulkUpdate and BulkDelete in your application.

Step 1: Create a project in Microsoft Visual Studio

Visual Studio start window with Create a new project selected

Select the project type ASP.NET Core Web App (Model-View-Controller).

Selecting the ASP.NET Core Web App (Model-View-Controller) template in Visual Studio

Enter the name of your project.

Entering the project name in the Configure your new project dialog

Choose a suitable framework version and tick the Configure for HTTPS checkbox.

Choosing the .NET framework version and Configure for HTTPS option

Your Solution Explorer looks like the image below.

Solution Explorer of the new ASP.NET Core MVC project

Step 2: Install the required packages from the NuGet Package Manager

Opening NuGet Package Manager to install the required packages

Select Microsoft.EntityFrameworkCore.SqlServer with a compatible version and install it.

Installing the Microsoft.EntityFrameworkCore.SqlServer NuGet package

Select Microsoft.EntityFrameworkCore.Tools with a compatible version and install it.

Installing the Microsoft.EntityFrameworkCore.Tools NuGet package

Select Microsoft.EntityFrameworkCore with a compatible version and install it.

Installing the Microsoft.EntityFrameworkCore NuGet package

Select EFCore.BulkExtensions with a compatible version and install it.

Installing the EFCore.BulkExtensions NuGet package

You can check the installed packages, as shown in the image below.

Installed EFCore.BulkExtensions and Entity Framework Core packages

Step 3: Create a Department model for bulk inserting, updating and deleting data.

public class Department
   {
       [Key]
       public int DepartmentId { get; set; }
       public string DepartmentName { get; set; }
       public int  EmployeesCount{ get; set; }
   } 

Step 4: Create an AppDbContext class and add the Department model class as a DbSet, as shown in the image and code below.

public class AppDbContext : DbContext
   {
       public AppDbContext(DbContextOptions options)
       : base(options)
       {

       }
       public DbSet<Department> tblDepartments { get; set; }
   }  

Step 5: Create an interface IDepartment in a Services folder and declare the three methods for bulk insert, update and delete, as shown in the code below.

public interface IDepartment
  {
      void BulkInsert();
      void BulkUpdate();
      void BulkDelete();
  } 

Step 6: Create a service class DepartmentService in the Services folder, implement the IDepartment interface, and provide the implementation of all three methods declared in it, as shown in the code below.

public class DepartmentService : IDepartment
  {
      AppDbContext db=null;
      public DepartmentService(AppDbContext _db)
      {
          db = _db;
      }       
      public void BulkDelete()
      {
          var departments = db.tblDepartments.ToList();

          db.BulkDelete(departments);
      }

      public void BulkUpdate()
      {
          var departments = db.tblDepartments.ToList();
          departments.ForEach(x => x.EmployeesCount = 50);
          db.BulkUpdate(departments);
      }
      private int GenerateRandomEmployeesCount()
      {
          Random rnd = new Random();
          int count = rnd.Next(25, 40);
          return count;
      }
      public void BulkInsert()
      {
          var departments = new List<Department>();
          for (int i = 1; i < 15; i++)
          {
              var depart = new Department()
              {
                  DepartmentId = i,
                  DepartmentName = "DepartmentNumber" + i,
                  EmployeesCount = GenerateRandomEmployeesCount()
              };
              departments.Add(depart);
          }
          db.BulkInsert(departments);
          db.SaveChanges();
      }
  } 
DepartmentService class using EFCore.BulkExtensions for bulk operations
BulkInsert method adding department records with db.BulkInsert()

Step 7: Define the connection string in the appsettings.json file, as shown in the image below.

SQL Server connection string defined in appsettings.json

Step 8: Register the DbContext in the Program.cs file, as shown in the image below.

Registering the DbContext and department service in Program.cs

Step 9: Run the Add-Migration "FirstMigration" command, as shown in the image below.

Running Add-Migration FirstMigration in Package Manager Console

Step 10: Run the Update-Database command, as shown in the image below.

Running Update-Database in Package Manager Console

After running the migration commands above, you can see a new Migrations folder in the solution, with an auto-generated FirstMigration class, as shown in the image below.

Migrations folder with the auto-generated FirstMigration class

Step 11: Create a controller named PracticeController with an action method named Index, and write three methods for BulkInsert, BulkUpdate and BulkDelete, as shown in the image and code below.

public class PracticeController : Controller
    {
        public IDepartment department=null;
        public PracticeController(IDepartment depart)
        {
            department= depart;
    }
        public IActionResult Index()
        {
            return View();
        }
        public void BulkInsert()
        {
            department.BulkInsert();
        }
        public void BulkUpdate()
        {
            department.BulkUpdate();
        }
        public void BulkDelete()
        {
            department.BulkDelete();
        }
    }
Controller actions calling BulkInsert, BulkUpdate and BulkDelete

Step 12: Create a view and write the HTML code below in it, as shown in the image.

Razor view with Bulk Insert, Bulk Update and Bulk Delete buttons

Now run the application. You will see three buttons, Bulk Insert, Bulk Update and Bulk Delete, as shown in the image below.

Running app showing the Bulk Insert, Bulk Update and Bulk Delete buttons

Click the Bulk Insert button; it inserts the department records into the database, as you can see in the image below.

Department records inserted into the database by BulkInsert

Click the Bulk Update button; it updates all the department records in the database, as you can see in the image below.

Department records updated in the database by BulkUpdate

Click the Bulk Delete button; it deletes all the department records from the database, as you can see in the image below.

Departments table after all records are removed by BulkDelete