ClassLibrary.EFCore
1.0.26
See the version list below for details.
dotnet add package ClassLibrary.EFCore --version 1.0.26
NuGet\Install-Package ClassLibrary.EFCore -Version 1.0.26
<PackageReference Include="ClassLibrary.EFCore" Version="1.0.26" />
paket add ClassLibrary.EFCore --version 1.0.26
#r "nuget: ClassLibrary.EFCore, 1.0.26"
// Install ClassLibrary.EFCore as a Cake Addin #addin nuget:?package=ClassLibrary.EFCore&version=1.0.26 // Install ClassLibrary.EFCore as a Cake Tool #tool nuget:?package=ClassLibrary.EFCore&version=1.0.26
NET8 EFCore Generic Repository
Collection of a generic implementation of Entity Framework Core for .NET 8 mostly used in my private and/or work projects thus avoiding the duplication of repetitive code.
Give a star
If you found this Implementation helpful or used it in your Projects, do give it a ⭐ on Github. Thanks!
Installation
The library is available on NuGet or run the following command in the .NET CLI:
dotnet add package ClassLibrary.EFCore
How to use
Registering services
services.AddDbContext<YourDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped<DbContext, YourDbContext>();
services.AddScoped<IRepository<YourEntity, YourTypeEntityId>, Repository<YourEntity, YourTypeEntityId>>();
Alternatively the generic version of the repository can be registered as follows:
services.AddScoped(typeof(IRepository<,>), typeof(Repository<,>));
Important
In this README the INT type is used as ID type, but it is also possible to use the GUID type, making the appropriate corrections later.
Example entity
public class YourEntity : IEntity<int>
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
Example interface
public interface IYourEntityService
{
Task<IQueryable<YourEntity>> GetAllAsync(Func<IQueryable<YourEntity>,
IIncludableQueryable<YourEntity, object>> includes = null!,
Expression<Func<YourEntity, bool>> filter = null!,
Expression<Func<YourEntity, object>> orderBy = null!,
bool ascending = true);
Task<YourEntity> GetByIdAsync(int id);
Task CreateAsync(YourEntity entity);
Task UpdateAsync(YourEntity entity);
Task DeleteAsync(YourEntity entity);
//Alternative method for deleting
Task DeleteByIdAsync(int id);
//Optional method
Task<PaginatedResult<TEntity>> GetPaginatedAsync(IQueryable<TEntity> query, int pageNumber, int pageSize);
}
Example class
public class YourEntityService : IYourEntityService
{
private readonly IRepository<YourEntity, int> _repository;
public YourEntityService(IRepository<YourEntity, int> repository)
{
_repository = repository;
}
//This method accepts optional lambdas as input (includes, where, order by),
//while by default it is sorted in ascending order (set false if you want to sort in descending order)
public async Task<IQueryable<TEntity>> GetAllAsync()
{
return await _repository.GetAllAsync();
}
public async Task<YourEntity> GetByIdAsync(int id)
{
return await _repository.GetByIdAsync(id);
}
public async Task CreateAsync(YourEntity entity)
{
await _repository.CreateAsync(entity);
}
public async Task UpdateAsync(YourEntity entity)
{
await _repository.UpdateAsync(entity);
}
public async Task DeleteAsync(YourEntity entity)
{
await _repository.DeleteAsync(entity);
}
//Alternative method for deleting
public async Task DeleteByIdAsync(int id)
{
await _repository.DeleteByIdAsync(id);
}
//Optional method for pagination
public async Task<PaginatedResult<TEntity>> GetPaginatedAsync(IQueryable<TEntity> query, int pageNumber, int pageSize)
{
//For optional lambdas read the comments of the GetAllAsync method
var query = await repository.GetAllAsync();
var result = await repository.GetPaginatedAsync(query, 2, 5);
return result;
}
}
Contributing
Contributions and/or suggestions are always welcome.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- Microsoft.EntityFrameworkCore (>= 9.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.27 | 24 | 2/5/2025 |
1.0.26 | 27 | 2/5/2025 |
1.0.19 | 99 | 11/13/2024 |
1.0.17 | 97 | 11/8/2024 |
1.0.16 | 112 | 10/20/2024 |
1.0.14 | 95 | 10/9/2024 |
1.0.13 | 120 | 9/5/2024 |
1.0.12 | 142 | 8/14/2024 |
1.0.10 | 104 | 7/16/2024 |
1.0.9 | 117 | 6/8/2024 |
1.0.8 | 119 | 6/1/2024 |
1.0.7 | 113 | 5/18/2024 |
1.0.6 | 126 | 4/23/2024 |
1.0.4 | 123 | 4/13/2024 |
1.0.3 | 129 | 3/26/2024 |
1.0.2 | 125 | 3/20/2024 |
1.0.1 | 120 | 3/19/2024 |