Bulk extensions for Entity Framework Core.
Bulk operations typically reduce the number of database round-trips required. This make bulk operations can often be faster than calling DbContext.SaveChangesAsync()
.
The easiest way to install EFCoreExtras is via NuGet.
Install the library using the following .net cli command
dotnet add package EFCoreExtras
or in Visual Studio's Package Manager Console, enter the following command:
Install-Package EFCoreExtras
List<Employee> employees = [];
foreach (int i = 0; i < 1000; i++)
{
var employee = new Employee
{
Name = "Name",
Salary = 1000,
};
employees.Add(employee);
}
await context.BulkCreateAsync(employees, batchSize: 100);
INSERT INTO Employees (Id, Name, Salary)
VALUES (..., 'Name', 1000),
(..., 'Name', 1000),
(..., 'Name', 1000)
var employees = context.Employees.ToList();
foreach (var employee in employees)
{
employee.Name = $"{employee.Name} {employee.Id}"
employee.Salary += 1000;
}
await context.BulkUpdateAsync(employees, ["Name", "Salary"]);
// Or
await context.BulkUpdateAsync(employees, [e => e.Name, e => e.Salary]);
UPDATE Employees
SET Name = CASE
WHEN Id = 1 THEN '...'
WHEN Id = 2 THEN '...'
...
ELSE Name,
Salary = CASE
WHEN Id = 1 THEN ...
WHEN Id = 2 THEN ...
...
ELSE Salary
END
WHERE id IN (1, 2, ...)
var query = _dbContext.Employees.AsQueryable();
PaginatedItems<Employee> response = await Pagination.CreateAsync(pageIndex: 1, pageSize: 10, query);
return TypedResults.Ok(response);
{
"data": [
...
],
"pageIndex": 1,
"pageSize": 10,
"totalItems": 100,
"totalPages": 10,
"hasNextPage": true,
}
Your contributions are always welcome! If you find any bugs or have suggestions for improvement, please feel free to open an issue or a pull request and let's sort things out.