PolleriaJs.EntityFramework.Infrastructure.Core
1.0.1
See the version list below for details.
dotnet add package PolleriaJs.EntityFramework.Infrastructure.Core --version 1.0.1
NuGet\Install-Package PolleriaJs.EntityFramework.Infrastructure.Core -Version 1.0.1
<PackageReference Include="PolleriaJs.EntityFramework.Infrastructure.Core" Version="1.0.1" />
paket add PolleriaJs.EntityFramework.Infrastructure.Core --version 1.0.1
#r "nuget: PolleriaJs.EntityFramework.Infrastructure.Core, 1.0.1"
// Install PolleriaJs.EntityFramework.Infrastructure.Core as a Cake Addin #addin nuget:?package=PolleriaJs.EntityFramework.Infrastructure.Core&version=1.0.1 // Install PolleriaJs.EntityFramework.Infrastructure.Core as a Cake Tool #tool nuget:?package=PolleriaJs.EntityFramework.Infrastructure.Core&version=1.0.1
Entity Framework Core Repository and Unit of Work
Overview
This library provides a robust implementation of the Repository and Unit of Work patterns using Entity Framework Core. It abstracts database operations and provides a clean and testable way to interact with the database.
Features
- Generic repository implementation (
EntityRepository<TEntity>
) - Unit of Work for managing transactions (
UnitOfWork
) - Support for LINQ queries and raw SQL
- Easy-to-use API for CRUD operations
Installation
To use this library, ensure you have Entity Framework Core installed in your project. You can install it via NuGet package manager or the .NET CLI:
dotnet add package Microsoft.EntityFrameworkCore
Usage
Setting Up the DbContext
First, ensure you have a DbContext
implementation in your project.
public class MyDbContext : DbContext
{
// Define your DbSets
}
Repository Usage
The EntityRepository<TEntity>
can be used as follows:
// Instantiate your DbContext
var dbContext = new MyDbContext();
// Create a repository for an entity
var repository = new EntityRepository<MyEntity>(dbContext);
// Adding an entity
repository.Add(new MyEntity { /* Set properties */ });
// Querying entities
var entities = repository.AsQueryable().Where(/* some conditions */);
// Save changes
dbContext.SaveChanges();
Unit of Work Usage
The UnitOfWork
pattern is used to manage transactions:
var unitOfWork = new UnitOfWork(new MyDbContext());
// Start a transaction
unitOfWork.BeginTransaction();
try
{
// Perform database operations
var repository = unitOfWork.Repository<MyEntity>();
repository.Add(new MyEntity { /* Set properties */ });
// Commit the transaction
unitOfWork.Commit();
}
catch
{
// Rollback in case of an exception
unitOfWork.RollBack();
}
Additional Notes
- Ensure proper disposal of DbContext and UnitOfWork instances to release database connections.
- Customize the repository and unit of work classes to fit your specific needs.
About the Author
Selvin Medina
- GitHub: selvinmedina
- Linkedin: selvinmedina
- Contact: Email
Repository
- GitHub Repository: EntityFramework.Infrastructure.Core
- Issues and feature requests can be submitted through the GitHub issue tracker.
Contributing
Contributions are welcome. Please follow the coding conventions and add tests for new features.
License
MIT.
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 (>= 8.0.0)
- Microsoft.EntityFrameworkCore.Relational (>= 8.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.
First Version