Entity Framework Core implementation of RepositoryKit. Implements IRepository, IRepositoryQuery and IRepositoryBulk using DbContext and DbSet.
$ dotnet add package RepositoryKit.EntityFramework
Entity Framework Core Implementation for RepositoryKit
This package contains the Entity Framework Core based implementation of the RepositoryKit abstractions.
| Class | Purpose |
|---|---|
EfReadOnlyRepository<TEntity,TContext> | Read-only LINQ queries on your entities |
EfRepository<TEntity, TContext> | Full-featured CRUD repository for EF Core |
EfUnitOfWork<TContext> | Unit of Work for a single DbContext |
EfUnitOfWorkManager | Multi-context Unit of Work resolver via DI |
// Register DbContext and RepositoryKit services
builder.Services.AddDbContext<SampleDbContext>(opt => opt.UseInMemoryDatabase("SampleDb"));
builder.Services.AddScoped(typeof(IUnitOfWork<>), typeof(EfUnitOfWork<>));
builder.Services.AddSingleton<IUnitOfWorkManager, EfUnitOfWorkManager>();
// Custom repository registration (interface-based)
builder.Services.AddScoped<IProductRepository, ProductRepository>();
app.MapPost("/products", async (IUnitOfWork<SampleDbContext> uow, Product product) =>
{
var repo = uow.GetRepository<Product>();
await repo.AddAsync(product);
await uow.SaveChangesAsync();
return Results.Created($"/products/{product.Id}", product);
});
Custom Repository Interface and Implementation
public interface IProductRepository : IRepository<Product>
{
Task<List<Product>> GetExpensiveProductsAsync(decimal minPrice);
}
public class ProductRepository : EfRepository<Product, SampleDbContext>, IProductRepository
{
public ProductRepository(SampleDbContext context) : base(context) { }
public async Task<List<Product>> GetExpensiveProductsAsync(decimal minPrice)
{
return await _dbSet.Where(p => p.Price > minPrice).ToListAsync();
}
}app.MapGet("/products/expensive", async (IProductRepository repo, decimal minPrice) =>
{
var expensive = await repo.GetExpensiveProductsAsync(minPrice);
return Results.Ok(expensive);
});app.MapGet("/multi-context-demo", (IUnitOfWorkManager uowManager) =>
{
var uow = uowManager.GetUnitOfWork<SampleDbContext>();
var repo = uow.GetRepository<Product>();
// Use repo as needed...
});All repository and unit of work operations wrap provider or database exceptions with the standard RepositoryException:`
try
{
await repository.AddAsync(product);
await unitOfWork.SaveChangesAsync();
}
catch (RepositoryException ex) when (ex.ErrorType == RepositoryErrorType.Add)
{
// Handle or log rich error info
}MIT © Ataberk Kaya
📎 This package is the official EF Core provider for RepositoryKit