RA.Utilities.Data.EntityFramework
10.2.0
Prefix Reserved
See the version list below for details.
dotnet add package RA.Utilities.Data.EntityFramework --version 10.2.0
NuGet\Install-Package RA.Utilities.Data.EntityFramework -Version 10.2.0
<PackageReference Include="RA.Utilities.Data.EntityFramework" Version="10.2.0" />
<PackageVersion Include="RA.Utilities.Data.EntityFramework" Version="10.2.0" />
<PackageReference Include="RA.Utilities.Data.EntityFramework" />
paket add RA.Utilities.Data.EntityFramework --version 10.2.0
#r "nuget: RA.Utilities.Data.EntityFramework, 10.2.0"
#:package RA.Utilities.Data.EntityFramework@10.2.0
#addin nuget:?package=RA.Utilities.Data.EntityFramework&version=10.2.0
#tool nuget:?package=RA.Utilities.Data.EntityFramework&version=10.2.0
RA.Utilities.Data.EntityFramework
This package provides concrete implementations of the repository and unit of work patterns for Entity Framework Core, based on the abstractions from RA.Utilities.Data.Abstractions. It's designed to accelerate the setup of a data access layer in a clean, testable, and maintainable way. It also maps the value objects from RA.Utilities.Core.ValueObjects (Email, Currency, SSN, IBAN, Money) to database columns.
✨ Key Features
- Generic Repository Implementations: Provides ready-to-use base classes for repository patterns, saving you from writing boilerplate CRUD (Create, Read, Update, Delete) code.
- Command Query Separation (CQS): Offers distinct
ReadRepositoryBase<T, TKey>andWriteRepositoryBase<T, TKey>classes to help you build a clean architecture where read and write operations are separated. - Performance-Optimized Queries:
ReadRepositoryBase<T, TKey>.ListAsyncusesAsNoTracking()for more efficient data retrieval. - Automatic Timestamping: Includes a
BaseEntitySaveChangesInterceptorthat automatically sets theCreatedAtandLastModifiedAtproperties on entities that expose them (theBaseEntity<TKey>andWriteEntity<TKey>base classes) when they are saved. - Simplified Dependency Injection: Provides extension methods to register your repositories with a single line of code in
Program.cs. - Value Object Mapping: EF Core value converters and the
HasMoneyconvention map the value objects fromRA.Utilities.Core.ValueObjects(Email,Currency,SSN,IBAN,Money) to database columns — aMoneycan be stored as two columns (amount + currency) or as a single JSON string column.
Installation
You can install the package via the .NET CLI:
dotnet add package RA.Utilities.Data.EntityFramework
Install-Package RA.Utilities.Data.EntityFramework
Core Components
Repository Implementations
This package provides concrete implementations for the interfaces defined in RA.Utilities.Data.Abstractions.
RepositoryBase<T, TKey>: A full-featured generic repository that implementsIRepositoryBase<T, TKey>for complete CRUD functionality. It's a convenient all-in-one solution for services that need to both query and modify data.ReadRepositoryBase<T, TKey>: A read-only repository implementingIReadRepositoryBase<T, TKey>.ListAsyncusesAsNoTracking()for performance, making it ideal for the "Query" side of a CQRS architecture.WriteRepositoryBase<T, TKey>: A write-only repository implementingIWriteRepositoryBase<T, TKey>. It is designed for "Command" operations like adding, updating, and deleting entities.
Interceptors
BaseEntitySaveChangesInterceptor: An Entity Framework Core interceptor that automatically populates timestamp properties (CreatedAt,LastModifiedAt) on entities exposing them (theBaseEntity<TKey>andWriteEntity<TKey>base classes) before changes are saved to the database. This ensures consistent and accurate auditing without manual intervention.
Value Object Mapping
The Conventions namespace contains EF Core value converters for the value objects of RA.Utilities.Core.ValueObjects:
CurrencyConverter/EmailConverter/SsnConverter/IbanConverter: map the string-based value objects to and from their normalizedValuestring.MoneyConverter: serializes the wholeMoneyas JSON into a single string column.
using RA.Utilities.Data.EntityFramework.Conventions;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>(builder =>
{
builder.Property(c => c.Email).HasConversion(new EmailConverter());
builder.Property(c => c.Iban).HasConversion(new IbanConverter());
});
}
The MoneyConventionExtensions.HasMoney extension maps a Money property to two separate database columns instead — a decimal Amount column and a string Currency column (through CurrencyConverter). EF Core value converters can only map a property to a single column, so HasMoney maps the property as an EF Core complex property:
using RA.Utilities.Data.EntityFramework.Extensions;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Wallet>().HasMoney(w => w.Balance);
}
- Columns are named after the property, e.g.
Balance_AmountandBalance_Currency; theAmountcolumn uses the provider default fordecimal(decimal(18,2)on SQL Server,TEXTon SQLite). - Supports required (
Money), optional (Money?, stored asNULLin both columns) and multipleMoneyproperties on the same entity. - Because
Moneyis immutable, updates persist by replacing the instance on a tracked entity. - Returns the
ComplexPropertyBuilder<Money>, so column names and other facets can still be configured.
Dependency Injection Extensions
The ServiceCollectionExtensions class simplifies the registration of the generic repositories in your application's DI container. The methods are declared as C# 14 extension members on IServiceCollection, so the calling syntax is unchanged.
AddRepositoryBase(): Registers the fullIRepositoryBase<T, TKey>implementation.AddReadRepositoryBase(): Registers the read-onlyIReadRepositoryBase<T, TKey>implementation.AddWriteRepositoryBase(): Registers the write-onlyIWriteRepositoryBase<T, TKey>implementation.
Usage Example
Here is how you can configure and use the components from this package in an ASP.NET Core application.
1. Define Your DbContext
Define a normal EF Core DbContext with the entity sets you need. Implementing IDbContext from RA.Utilities.Data.Abstractions is optional — the repositories only require DbContext.
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
public DbSet<Product> Products { get; set; }
}
2. Register Services in Program.cs
In your Program.cs, register your DbContext, the interceptor, and the desired repository implementations.
using RA.Utilities.Data.EntityFramework.Extensions;
using RA.Utilities.Data.EntityFramework.Interceptors;
var builder = WebApplication.CreateBuilder(args);
// 1. Register the SaveChangesInterceptor
builder.Services.AddScoped<BaseEntitySaveChangesInterceptor>();
// 2. Register the DbContext and add the interceptor.
// Register it as the implementation of DbContext so the open-generic
// repositories can resolve their DbContext constructor parameter.
builder.Services.AddDbContext<DbContext, ApplicationDbContext>((provider, options) =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))
.AddInterceptors(provider.GetRequiredService<BaseEntitySaveChangesInterceptor>()));
// 3. Register the generic repositories
builder.Services.AddRepositoryBase(); // For IRepositoryBase<T, TKey>
builder.Services.AddReadRepositoryBase(); // For IReadRepositoryBase<T, TKey>
builder.Services.AddWriteRepositoryBase(); // For IWriteRepositoryBase<T, TKey>
// Register your custom repositories
builder.Services.AddScoped<IProductRepository, ProductRepository>();
var app = builder.Build();
3. Implement a Custom Repository (Optional)
You can extend the generic repositories to add custom data access methods.
public interface IProductRepository : IRepositoryBase<Product, Guid>
{
Task<Product?> GetProductByNameAsync(string name);
}
public class ProductRepository : RepositoryBase<Product, Guid>, IProductRepository
{
private readonly ApplicationDbContext _dbContext;
public ProductRepository(ApplicationDbContext dbContext) : base(dbContext)
{
_dbContext = dbContext;
}
public async Task<Product?> GetProductByNameAsync(string name)
{
return await _dbContext.Products.FirstOrDefaultAsync(p => p.Name == name);
}
}
4. Use in a Service
Finally, inject the repository interfaces into your services to interact with the database.
public class ProductService
{
private readonly IProductRepository _productRepository;
public ProductService(IProductRepository productRepository)
{
_productRepository = productRepository;
}
public async Task<Product?> GetProductDetailsAsync(Guid id)
{
// Use a method from the base repository
return await _productRepository.GetByIdAsync(id);
}
public async Task CreateProductAsync(string name, decimal price)
{
var newProduct = new Product { Name = name, Price = price };
// The BaseEntitySaveChangesInterceptor will automatically set CreatedAt
await _productRepository.AddAsync(newProduct);
// Write operations are only persisted when SaveChangesAsync is called.
await _productRepository.SaveChangesAsync();
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- Microsoft.EntityFrameworkCore (>= 10.0.12)
- RA.Utilities.Core.ValueObjects (>= 10.2.0)
- RA.Utilities.Data.Abstractions (>= 10.0.5)
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 |
|---|---|---|
| 10.2.1 | 123 | 10/19/2026 |
| 10.2.0 | 103 | 10/18/2026 |
| 10.1.0 | 103 | 10/15/2026 |
| 10.0.9-rc | 184 | 11/8/2025 |
| 10.0.8-rc | 167 | 11/8/2025 |
| 10.0.4-rc | 167 | 11/8/2025 |
| 10.0.3-rc | 174 | 11/8/2025 |
| 10.0.2 | 119 | 9/27/2026 |
| 10.0.2-rc.2 | 171 | 11/8/2025 |
| 10.0.1 | 307 | 12/14/2025 |
| 10.0.1-rc.2 | 213 | 11/5/2025 |
| 10.0.0 | 260 | 11/24/2025 |
| 10.0.0-rc.2 | 225 | 10/30/2025 |