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
Select the project type ASP.NET Core Web App (Model-View-Controller).
Enter the name of your project.
Choose a suitable framework version and tick the Configure for HTTPS checkbox.
Your Solution Explorer looks like the image below.
Step 2: Install the required packages from the NuGet Package Manager
Select Microsoft.EntityFrameworkCore.SqlServer with a compatible version and install it.
Select Microsoft.EntityFrameworkCore.Tools with a compatible version and install it.
Select Microsoft.EntityFrameworkCore with a compatible version and install it.
Select EFCore.BulkExtensions with a compatible version and install it.
You can check the installed packages, as shown in the image below.
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();
}
}
Step 7: Define the connection string in the appsettings.json file, as shown in the image below.
Step 8: Register the DbContext in the Program.cs file, as shown in the image below.
Step 9: Run the Add-Migration "FirstMigration" command, as shown in the image below.
Step 10: Run the Update-Database command, as shown in the image below.
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.
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();
}
}
Step 12: Create a view and write the HTML code below in it, as shown in the image.
Now run the application. You will see three buttons, Bulk Insert, Bulk Update and Bulk Delete, as shown in the image below.
Click the Bulk Insert button; it inserts the department records into the database, as you can see in the image below.
Click the Bulk Update button; it updates all the department records in the database, as you can see in the image below.
Click the Bulk Delete button; it deletes all the department records from the database, as you can see in the image below.