Skip to content

madeyoga/EFCoreExtras

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EFCoreExtras

NuGet Version GitHub License

Bulk extensions for Entity Framework Core.

Features

Bulk operations

Bulk operations typically reduce the number of database round-trips required. This make bulk operations can often be faster than calling DbContext.SaveChangesAsync().

Installing via NuGet

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

Usage

BulkCreateAsync

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)

BulkUpdateAsync

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, ...)

Pagination class

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,
}

Contribution

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.

About

Bulk extensions for Entity Framework Core

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages