EF.Core.Repositories
1.0.1
See the version list below for details.
dotnet add package EF.Core.Repositories --version 1.0.1
NuGet\Install-Package EF.Core.Repositories -Version 1.0.1
<PackageReference Include="EF.Core.Repositories" Version="1.0.1" />
<PackageVersion Include="EF.Core.Repositories" Version="1.0.1" />
<PackageReference Include="EF.Core.Repositories" />
paket add EF.Core.Repositories --version 1.0.1
#r "nuget: EF.Core.Repositories, 1.0.1"
#:package EF.Core.Repositories@1.0.1
#addin nuget:?package=EF.Core.Repositories&version=1.0.1
#tool nuget:?package=EF.Core.Repositories&version=1.0.1
EF.Core.Repositories
Extends Entity Framework Core to provide a Repository Pattern based on LINQ.
Installing
Install NuGet package from NuGet.org
$ dotnet add package EF.Core.Repositories
Requirements
- .NET 7 or higher
- Entity Framework Core 7.0.14+
Getting Started
The Repository Factory can be registered by calling ConfigureData() extension.
The Action<DbContextOptionsBuilder> will be passed to AddDbContextFactory() extension provided by Entity Framework Core.
Note Due to the support for multiple asynchronous actions being executed against one or more repositories, multiple contexts may be created, and leveraging a DbContextFactory is required.
using EF.Core.Repositories;
...
services.ConfigureData<TContext>(opt => ...);
Within a Service/Controller/etc.
public class Consumer
{
private readonly IRepositoryFactory<TContext> _factory;
public Consumer(IRepositoryFactory<TContext> factory)
{
_factory = factory;
}
...
public async Task Command()
{
var repository = _factory.GetRepository<TEntity>();
...
}
}
Usage
IReadOnlyRepository LINQ Extensions
The majority of IQueryable Extensions are also available for IReadOnlyRepository.
- Distinct()
- Except()
- GroupBy()
- Intersect()
- IntersectBy()
- Join()
- OrderBy()
- OrderByDescending()
- Select()
- SelectMany()
- Skip()
- SkipLast()
- SkipWhile()
- Take()
- TakeLast()
- TakeWhile()
- ThenBy()
- ThenByDescending()
- Union()
- UnionBy()
- Where()
- Zip()
IReadOnlyRepository Async Extensions
Additional Extensions that will enumerate the Repository against the DbContext.
- AllAsync()
- AnyAsync()
- AverageAsync()
- CountAsync()
- FirstAsync()
- FirstOrDefaultAsync()
- GetAsync()
- Returns an IEnumerable containing the entities of the Repository.
- LastAsync()
- LastOrDefaultAsync()
- LongCountAsync()
- MaxAsync()
- MinAsync()
- SingleAsync()
- SingleOrDefaultAsync()
- SumAsync()
IRepository Extensions
IRepository has all the extensions available to IReadOnlyRepository and adds the following.
- Include()
- ThenInclude()
Include()
Just like when working with a DbSet exposed by a DbContext, you can include loading navigation properties of your entities.
Ex. Retrieving all Users, their UserRoles, and the subsequent referenced Role
var repository = _factory
.GetRepository<User>()
.Include(x => x.UserRoles)
.ThenInclude(x => x.Role);
var users = await repository.GetAsync();
IRepository Async Extensions
IRepository has all the async extensions available to IReadOnlyRepository and adds the following.
- DeleteAsync(entity)
- DeleteByIdAsync(key)
- DeleteRangeAsync(entities)
- DeleteRangeByIdAsync(keys)
- GetAsync(key)
- InsertAsync(entity)
- InsertRangeAsync(entities)
- UpdateAsync(entity)
- UpdateRangeAsync(entities)
GetAsync(key) and DeleteByIdAsync(key)
These functions allow you to retreive/remove an entity based upon primary key data.
Ex. Retrieving a User with a simple primary key of (int UserId)
var repository = _factory.GetRepository<User>();
var user = await repository.GetAsync(new { UserId = 5 });
Ex. Retrieving a UserRole with a complex primary key of (int UserId, int RoleId)
var repository = _factory.GetRepository<UserRole>();
var userrole = await repository.GetAsync(new { UserId = 5, RoleId = 1 });
UpdateAsync(entity)
By default UpdateAsync will ignore all navigation properties and only update the concrete data fields within the entity.
To include a navigation property as part of the update use the Include() extension.
Ex. Adding a UserRole to a User
var repository = _factory.GetRepository<User>();
var user = await repository
.Include(x => x.UserRoles)
.GetAsync(new { UserId = 5 });
user.UserRoles.Add(
new UserRole
{
UserId = 5,
RoleId = 2,
...
});
var result = await repository
.Include(x => x.UserRoles)
.UpdateAsync(user);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net7.0
- Microsoft.EntityFrameworkCore (>= 7.0.14)
- System.Reflection.Emit (>= 4.7.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on EF.Core.Repositories:
Package | Downloads |
---|---|
EF.Core.Repositories.Test
Provides testing capabilities for EF.Core.Repositories. |
GitHub repositories
This package is not used by any popular GitHub repositories.