RA.Utilities.Data.EntityFramework 10.2.0

Prefix Reserved
There is a newer version of this package available.
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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RA.Utilities.Data.EntityFramework" Version="10.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RA.Utilities.Data.EntityFramework" Version="10.2.0" />
                    
Directory.Packages.props
<PackageReference Include="RA.Utilities.Data.EntityFramework" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RA.Utilities.Data.EntityFramework --version 10.2.0
                    
#r "nuget: RA.Utilities.Data.EntityFramework, 10.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RA.Utilities.Data.EntityFramework@10.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RA.Utilities.Data.EntityFramework&version=10.2.0
                    
Install as a Cake Addin
#tool nuget:?package=RA.Utilities.Data.EntityFramework&version=10.2.0
                    
Install as a Cake Tool

RA.Utilities.Data.EntityFramework

NuGet version Codecov NuGet Downloads Documentation GitHub license

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> and WriteRepositoryBase<T, TKey> classes to help you build a clean architecture where read and write operations are separated.
  • Performance-Optimized Queries: ReadRepositoryBase<T, TKey>.ListAsync uses AsNoTracking() for more efficient data retrieval.
  • Automatic Timestamping: Includes a BaseEntitySaveChangesInterceptor that automatically sets the CreatedAt and LastModifiedAt properties on entities that expose them (the BaseEntity<TKey> and WriteEntity<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 HasMoney convention map the value objects from RA.Utilities.Core.ValueObjects (Email, Currency, SSN, IBAN, Money) to database columns — a Money can 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 implements IRepositoryBase<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 implementing IReadRepositoryBase<T, TKey>. ListAsync uses AsNoTracking() for performance, making it ideal for the "Query" side of a CQRS architecture.
  • WriteRepositoryBase<T, TKey>: A write-only repository implementing IWriteRepositoryBase<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 (the BaseEntity<TKey> and WriteEntity<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 normalized Value string.
  • MoneyConverter: serializes the whole Money as 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_Amount and Balance_Currency; the Amount column uses the provider default for decimal (decimal(18,2) on SQL Server, TEXT on SQLite).
  • Supports required (Money), optional (Money?, stored as NULL in both columns) and multiple Money properties on the same entity.
  • Because Money is 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 full IRepositoryBase<T, TKey> implementation.
  • AddReadRepositoryBase(): Registers the read-only IReadRepositoryBase<T, TKey> implementation.
  • AddWriteRepositoryBase(): Registers the write-only IWriteRepositoryBase<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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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