Chúng ta có thể chèn dữ liệu lớn nhanh hơn 20 lần so với cách chèn thông thường. Chúng ta thử xem so sánh bên dưới

- Project Setup
Đầu tiên sau khi tạo một Project thì tiếp ngay sau đó thì chúng ta cài đặt những thư viện cần dùng đến trong project này . Nhớ là đồng bộ theo đúng 1 version nhá
Bước 1: Cài đặt các Nuget Package
Bước 2: Tạo một Model :
Tạo một mô hình và DbContext nơi chúng ta có thể thực hiện việc tạo bảng trong Cơ sở dữ liệu SQL với thiết lập cấu hình.using System.ComponentModel.DataAnnotations;
namespace BulkSolution.Entities
{
public class Employee
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Designation { get; set; }
public string City { get; set; }
}
}using BulkSolution.Entities;
using Microsoft.EntityFrameworkCore;
namespace BulkSolution.EF
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions options) : base(options)
{
}
public DbSet Employees { get; set; }
}
}
Bước 4: Cấu hình ở appsetting.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=CNPM-PC\\MSSQLSERVER01;Database=DBBulk;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Bước 5: Ở Program.cs và thêm đoạn code sau vào bên trong :
services.AddDbContext(options =>options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddScoped ();
- Add-Migration InitialCreate
- Update-database
Chúng ta sẽ tạo theo như hình dưới là oke
Bước 7: Chúng ta tạo tầng Services:
using BulkSolution.EF;
using BulkSolution.Entities;
using EFCore.BulkExtensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BulkSolution.Services
{
public class EmployeeService
{
private readonly ApplicationDbContext _appDbContext;
private DateTime Start;
private TimeSpan TimeSpan;
//The "duration" variable contains Execution time when we doing the operations (Insert,Update,Delete)
public EmployeeService(ApplicationDbContext appDbContext)
{
_appDbContext = appDbContext;
}
#region Add Bulk Data
public async Task AddBulkDataAsync()
{
List employees = new(); // C# 9 Syntax.
Start = DateTime.Now;
for (int i = 0; i < 100000; i++)
{
employees.Add(new Employee()
{
Name = "Employee_" + i,
Designation = "Designation_" + i,
City = "City_" + i
});
}
await _appDbContext.BulkInsertAsync(employees);
TimeSpan = DateTime.Now - Start;
return TimeSpan;
}
#endregion
#region Update Bulk Data
public async Task UpdateBulkDataAsync()
{
List employees = new(); // C# 9 Syntax.
Start = DateTime.Now;
for (int i = 0; i < 100000; i++)
{
employees.Add(new Employee()
{
Id = (i + 1),
Name = "Update Employee_" + i,
Designation = "Update Designation_" + i,
City = "Update City_" + i
});
}
await _appDbContext.BulkUpdateAsync(employees);
TimeSpan = DateTime.Now - Start;
return TimeSpan;
}
#endregion
#region Delete Bulk Data
public async Task DeleteBulkDataAsync()
{
List employees = new(); // C# 9 Syntax.
Start = DateTime.Now;
employees = _appDbContext.Employees.ToList();
await _appDbContext.BulkDeleteAsync(employees);
TimeSpan = DateTime.Now - Start;
return TimeSpan;
}
#endregion
}
}
using BulkSolution.Services;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace BulkSolution.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class BulkController : Controller
{
private readonly EmployeeService _employeeService;
public BulkController(EmployeeService employeeService)
{
_employeeService = employeeService;
}
[HttpPost(nameof(AddBulkData))]
public async Task AddBulkData()
{
var response = await _employeeService.AddBulkDataAsync();
return Ok(response);
}
[HttpPut(nameof(UpdateBulkData))]
public async Task UpdateBulkData()
{
var response = await _employeeService.UpdateBulkDataAsync();
return Ok(response);
}
[HttpDelete(nameof(DeleteBulkData))]
public async Task DeleteBulkData()
{
var response = await _employeeService.DeleteBulkDataAsync();
return Ok(response);
}
}
}



