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

Problem Defination :- We have a requirement in which we need to Insert, Update and Delete Bulk data into the SQL server database in ASP .Net Core application. For example I need to insert 50 Employees record into database in one go only , without loop through the Employee List then we can use BulkInsert in ASP core using EFCore.BulkExtensions nuget packages.

Below is the step by step process explain in details so that you can follow it and insert update and delete bulk data into database using BulkInsert, BulkUpdate and BulkDelete in your application.

Step 1 :- Create Project using Microsoft Visual Studio

Image is not available

Select Project Type ASP.Net Core Web App(Model-View-Controller)

Image is not available

Write Name of Your Application Project

Image is not available

Choose Suitable framework version and Check the checkbox for Configure Https

Image is not available

Your Solution explorer looks like as in image

Image is not available

Step 2 :- Install the required packages from nuget package manager

Image is not available

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

Image is not available

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

Image is not available

Select Microsoft.EntityFrameworkCore with compatible version and install it.

Image is not available

Select EFCore.BulkExtensions with compatible version and install it.

Image is not available

You can check installed packages as shown in below image

Image is not available

Step 3 :- Create a Model Department for bulk insert update and delete data to database.

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

Step 4 :- Create a AppDbContext class for Dbcontext and use Department model class as a DBSet as shown in below image and code.

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

        }
        public DbSet tblDepartments { get; set; }
    }  

Step 3 :- Create a Interfcae IDepartment into a Services folder and define the three methods for bulk insert update and delete in interface as shown in below code..

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

Step 3 :- Create a Service Class DepartmentService into a Services folder and implement the IDepartment Interface into it and provides implementation for all three methods for bulk insert update and delete that is present in interface IDepartment as shown in below code..

                    
  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();
            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();
        }
    } 
Image is not available
Image is not available

Step 5 :- Define a Connection string into Appsettings.Json file as shown in below image.

Image is not available

Step 6 :- Add a Middileware for DBContext in Program.cs file as shown in below image.

Image is not available

Step 7 :- Trigger the Add-Migration "FirstMigration" Command as shown in below image.

Image is not available

Step 8 :- Trigger the Update-Database Command as shown in below image.

Image is not available

After triggering above migration commands you can see a newly generated migration folder in solution and there is a FirstMigration Class is autogenerated from migration as shown in below image.

Image is not available

Step 9 :- Create a Controller and Named it PracticeController and inside controller create one action Method named it Index and write down three methods for BulkInsert,BulkUpdate and BulkDelete as shown in below image and code example.

                    
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();
        }
    }
  
Image is not available

Step 10 :- Create a View and write the below Html code into it as shown in image.

Image is not available

Now run the application you will get the three buttons for BulkInsert BulkUpdate and BulkDelete button on GUI as output that is shown in below image.

Image is not available

Click on the Bulk Insert button and it will insert departments record into the database as you can see in below image.

Image is not available

Click on the Bulk Update button and it will Update all the departments record into the database as you can see in below image.

Image is not available

Click on the Bulk Delete button and it will Delete all the departments record from the database as you can see in below image.

Image is not available

Image is not available

About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server, EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.