EntegratorPro.EntityFrameworkCore
1.0.3
dotnet add package EntegratorPro.EntityFrameworkCore --version 1.0.3
NuGet\Install-Package EntegratorPro.EntityFrameworkCore -Version 1.0.3
<PackageReference Include="EntegratorPro.EntityFrameworkCore" Version="1.0.3" />
paket add EntegratorPro.EntityFrameworkCore --version 1.0.3
#r "nuget: EntegratorPro.EntityFrameworkCore, 1.0.3"
// Install EntegratorPro.EntityFrameworkCore as a Cake Addin #addin nuget:?package=EntegratorPro.EntityFrameworkCore&version=1.0.3 // Install EntegratorPro.EntityFrameworkCore as a Cake Tool #tool nuget:?package=EntegratorPro.EntityFrameworkCore&version=1.0.3
EfRepository.cs
Overview
EfRepository
is a generic Entity Framework Core repository implementation that simplifies data access. It is designed to work with DbContext
and provides base methods for common database operations, such as retrieving, inserting, updating, and deleting entities.
This implementation adheres to the guideline of making repositories reusable by applying generic type constraints. All operations are asynchronous and support CancellationToken
for improved performance in asynchronous programming.
Features
- Generic Support: Works with any entity implementing
IEntity<TKey>
and any primary key type that isIEquatable<TKey>
. - Asynchronous Operations: All methods are asynchronous, ensuring efficiency.
- Common CRUD Operations:
- Fetching data with optional predicates.
- Checking for the existence of entities.
- Inserting, updating, and deleting single or multiple items.
- No-Tracking Queries: Provides support for read-only queries with
.AsNoTracking
for improved performance when updates are not needed. - SQL Execution: Executes raw SQL commands with
ExecuteSqlRawAsync
.
Methods Overview
Public Methods
Read Operations:
GetAllAsync()
: Fetches all entities or those matching a predicate.GetAsync()
: Retrieves a single entity by ID.AnyAsync()
: Checks if an entity with a given ID exists.
Create Operations:
InsertAsync()
: Inserts a single entity.InsertRangeAsync()
: Inserts multiple entities.
Update Operations:
UpdateAsync()
: Updates a single entity.UpdateRangeAsync()
: Updates multiple entities.
Delete Operations:
DeleteAsync()
: Deletes a single entity.DeleteRangeAsync()
: Deletes multiple entities.
SQL Execution:
ExecuteSqlRawAsync()
: Executes raw SQL commands.
Protected Members
Context and DbSet:
_context
: TheDbContext
instance._entities
: TheDbSet<TEntity>
representing the entity's table.
Save Changes:
SaveChangesAsync()
: Commits changes to the database asynchronously.
How to Use
Extend the Repository:
- Create a repository class that derives from
EfRepository
and provide the specific entity and key types:public class UserRepository : EfRepository<UserEntity, Guid> { public UserRepository(ApplicationDbContext context) : base(context) { } }
- Create a repository class that derives from
DI Configuration:
- Register your repository with your DI container to use it across the application:
services.AddScoped<IRepository<UserEntity, Guid>, UserRepository>();
- Register your repository with your DI container to use it across the application:
Access the Repository:
- Use the injected repository to perform CRUD operations:
var users = await userRepository.GetAllAsync(); await userRepository.InsertAsync(newUser);
- Use the injected repository to perform CRUD operations:
Generic Constraints
TEntity
: Must implementIEntity<TKey>
and be a class.TKey
: Must beIEquatable<TKey>
, ensuring compatibility with EF Core requirements for keys.
Example Use Case
For an entity Product
with a Guid
as the primary key:
public class Product : IEntity<Guid>
{
public Guid Id { get; set; }
public string Name { get; set; }
}
// Repository
public class ProductRepository : EfRepository<Product, Guid>
{
public ProductRepository(MyDbContext context) : base(context) { }
}
// Usage
var products = await productRepository.GetAllAsync(p => p.Name.StartsWith("A"));
await productRepository.InsertAsync(new Product { Id = Guid.NewGuid(), Name = "Apple" });
Notes
- Ensure proper exception handling for methods throwing
ArgumentNullException
andNullReferenceException
. - Use
AsNoTracking
for scenarios where entities do not require change tracking, improving query performance.
Dependencies
- NuGet Packages:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Abstractions
License
No explicit license available. Adapt and use responsibly in compliance with your project’s licensing guidelines.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. 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. |
-
net9.0
- EntegratorPro.Core (>= 1.0.3)
- Microsoft.EntityFrameworkCore (>= 9.0.2)
- Microsoft.EntityFrameworkCore.SqlServer (>= 9.0.2)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 9.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.