ClassLibrary.EFCore
1.0.19
dotnet add package ClassLibrary.EFCore --version 1.0.19
NuGet\Install-Package ClassLibrary.EFCore -Version 1.0.19
<PackageReference Include="ClassLibrary.EFCore" Version="1.0.19" />
paket add ClassLibrary.EFCore --version 1.0.19
#r "nuget: ClassLibrary.EFCore, 1.0.19"
// Install ClassLibrary.EFCore as a Cake Addin #addin nuget:?package=ClassLibrary.EFCore&version=1.0.19 // Install ClassLibrary.EFCore as a Cake Tool #tool nuget:?package=ClassLibrary.EFCore&version=1.0.19
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<IEnumerable<YourEntity>> GetAllAsync();
Task<YourEntity> GetByIdAsync(int id);
Task CreateAsync(YourEntity entity);
Task UpdateAsync(YourEntity entity);
Task DeleteAsync(YourEntity entity);
//Alternative method for extracting all records
Task<IEnumerable<YourEntity>> GetAllEntitiesAsync(Func<YourEntity, bool> predicate)
//Alternative method for deleting
Task DeleteByIdAsync(int id);
//Optional method
Task<List<YourEntity>> GetPaginatedAsync(Func<IQueryable<YourEntity>,
IIncludableQueryable<YourEntity, object>> includes,
Expression<Func<YourEntity, bool>> conditionWhere,
Expression<Func<YourEntity, dynamic>> orderBy,
bool ascending, int pageIndex, int pageSize);
}
Example class
public class YourEntityService : IYourEntityService
{
private readonly IRepository<YourEntity, int> _repository;
public YourEntityService(IRepository<YourEntity, int> repository)
{
_repository = repository;
}
public async Task<IEnumerable<YourEntity>> 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 extracting all records
public async Task<IEnumerable<YourEntity>> GetAllEntitiesAsync(Func<YourEntity, bool> predicate)
{
return await _repository.GetAllEntitiesAsync(predicate);
}
//Alternative method for deleting
public async Task DeleteByIdAsync(int id)
{
await _repository.DeleteByIdAsync(id);
}
//Optional method for pagination
//If ascending is passed to true, the list is sorted in ascending order.
//If ascending is passed to false, the list is sorted in descending order.
public Task<List<YourEntity>> GetPaginatedAsync(Func<IQueryable<YourEntity>,
IIncludableQueryable<YourEntity, object>> includes, Expression<Func<YourEntity, bool>> conditionWhere,
Expression<Func<YourEntity, dynamic>> orderBy, bool ascending, int pageIndex, int pageSize)
{
return await _repository.GetPaginatedAsync(includes, conditionWhere, orderBy, ascending, pageIndex, pageSize);
}
}
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. |
-
net8.0
- Microsoft.EntityFrameworkCore (>= 9.0.0)
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.19 | 66 | 11/13/2024 |
1.0.17 | 72 | 11/8/2024 |
1.0.16 | 100 | 10/20/2024 |
1.0.14 | 83 | 10/9/2024 |
1.0.13 | 110 | 9/5/2024 |
1.0.12 | 131 | 8/14/2024 |
1.0.10 | 92 | 7/16/2024 |
1.0.9 | 106 | 6/8/2024 |
1.0.8 | 110 | 6/1/2024 |
1.0.7 | 102 | 5/18/2024 |
1.0.6 | 117 | 4/23/2024 |
1.0.4 | 114 | 4/13/2024 |
1.0.3 | 116 | 3/26/2024 |
1.0.2 | 118 | 3/20/2024 |
1.0.1 | 113 | 3/19/2024 |