RA.Utilities.Data.EntityFramework 10.0.1

Prefix Reserved
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package RA.Utilities.Data.EntityFramework --version 10.0.1
                    
NuGet\Install-Package RA.Utilities.Data.EntityFramework -Version 10.0.1
                    
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.0.1" />
                    
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.0.1" />
                    
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.0.1
                    
#r "nuget: RA.Utilities.Data.EntityFramework, 10.0.1"
                    
#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.0.1
                    
#: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.0.1
                    
Install as a Cake Addin
#tool nuget:?package=RA.Utilities.Data.EntityFramework&version=10.0.1
                    
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.

✨ 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> and WriteRepositoryBase<T> classes to help you build a clean architecture where read and write operations are separated.
  • Performance-Optimized Queries: The ReadRepositoryBase<T> uses AsNoTracking() by default for more efficient data retrieval.
  • Automatic Timestamping: Includes a BaseEntitySaveChangesInterceptor that automatically sets CreatedAt and LastModifiedAt properties on your entities when they are saved.
  • Simplified Dependency Injection: Provides extension methods to register your repositories with a single line of code in Program.cs.

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>: A full-featured generic repository that implements IRepositoryBase<T> for complete CRUD functionality. It's a convenient all-in-one solution for services that need to both query and modify data.
  • ReadRepositoryBase<T>: A read-only repository implementing IReadRepositoryBase<T>. It is optimized for performance by using AsNoTracking() on queries, making it ideal for the "Query" side of a CQRS architecture.
  • WriteRepositoryBase<T>: A write-only repository implementing IWriteRepositoryBase<T>. 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 inheriting from BaseEntity before changes are saved to the database. This ensures consistent and accurate auditing without manual intervention.

Dependency Injection Extensions

The DependencyInjectionExtensions class simplifies the registration of the generic repositories in your application's DI container.

  • AddRepositoryBase(): Registers the full IRepositoryBase<> implementation.
  • AddReadRepositoryBase(): Registers the read-only IReadRepositoryBase<> implementation.
  • AddWriteRepositoryBase(): Registers the write-only IWriteRepositoryBase<> 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

First, ensure your DbContext implements the IDbContext interface from RA.Utilities.Data.Abstractions.

public class ApplicationDbContext : DbContext, IDbContext
{
    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
builder.Services.AddDbContext<ApplicationDbContext>((provider, options) =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))
           .AddInterceptors(provider.GetRequiredService<BaseEntitySaveChangesInterceptor>()));

// 3. Register the generic repositories
builder.Services.AddRepositoryBase(); // For IRepositoryBase<>
builder.Services.AddReadRepositoryBase(); // For IReadRepositoryBase<>
builder.Services.AddWriteRepositoryBase(); // For IWriteRepositoryBase<>

// 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>
{
    Task<Product?> GetProductByNameAsync(string name);
}

public class ProductRepository : RepositoryBase<Product>, IProductRepository
{
    public ProductRepository(ApplicationDbContext dbContext) : base(dbContext)
    {
    }

    public async Task<Product?> GetProductByNameAsync(string name)
    {
        return await _dbSet.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);
    }
}
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.0.9-rc 113 11/8/2025
10.0.8-rc 99 11/8/2025
10.0.4-rc 96 11/8/2025
10.0.3-rc 98 11/8/2025
10.0.2-rc.2 106 11/8/2025
10.0.1 215 12/14/2025
10.0.1-rc.2 151 11/5/2025
10.0.0 188 11/24/2025
10.0.0-rc.2 150 10/30/2025