Alvecta.Foundation.Infrastructure.EF 9.0.86

There is a newer version of this package available.
See the version list below for details.
dotnet add package Alvecta.Foundation.Infrastructure.EF --version 9.0.86
                    
NuGet\Install-Package Alvecta.Foundation.Infrastructure.EF -Version 9.0.86
                    
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="Alvecta.Foundation.Infrastructure.EF" Version="9.0.86" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Alvecta.Foundation.Infrastructure.EF" Version="9.0.86" />
                    
Directory.Packages.props
<PackageReference Include="Alvecta.Foundation.Infrastructure.EF" />
                    
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 Alvecta.Foundation.Infrastructure.EF --version 9.0.86
                    
#r "nuget: Alvecta.Foundation.Infrastructure.EF, 9.0.86"
                    
#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 Alvecta.Foundation.Infrastructure.EF@9.0.86
                    
#: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=Alvecta.Foundation.Infrastructure.EF&version=9.0.86
                    
Install as a Cake Addin
#tool nuget:?package=Alvecta.Foundation.Infrastructure.EF&version=9.0.86
                    
Install as a Cake Tool

Alvecta.Foundation.Infrastructure.EF

The EF Core implementation of the persistence ports in Alvecta.Foundation.Application.Ports: a write face (unit of work + command repositories) and a read face (query session + queryable repositories), each over its own DbContext, plus the cross-cutting DbContext concerns, multi-tenancy, migrations and value-object conversion that go with them.

It belongs to the Infrastructure layer: a driven adapter that implements ports and never sees use cases. It is database-agnostic; Alvecta.Foundation.Infrastructure.EF.Postgres and Alvecta.Foundation.Infrastructure.EF.AuroraDsql add the provider wiring. Dependencies: Microsoft.EntityFrameworkCore (+ .Relational), Alvecta.Utils, Alvecta.Utils.DependencyInjection, Alvecta.Utils.Queryable (+ .EF), Alvecta.Foundation.Application.Ports and Alvecta.Foundation.Domain.ValueObjects.

What it contains

Contexts and concerns (Persistence, Persistence.Concerns)

  • ExtensibleDbContext — a DbContext that runs a list of IDbContextConcerns at each lifecycle stage (OnConfiguring, ConfigureConventions, OnModelCreating, OnSavingChangesAsync). Synchronous SaveChanges throws.
  • The standard concerns: ConcurrencyStampConcern (a ConcurrencyStamp shadow concurrency token on every entity, renewed on save), DateTimeOffsetUtcConcern (stores every DateTimeOffset as UTC), RequiredFieldValidationConcern (fails a save that leaves a required property null) and NoActionDeleteBehaviorConcern (foreign keys default to NoAction instead of cascading; owned types and an explicit OnDelete() are left alone).
  • The face concerns: ProjectionOnlyQueryConcern makes a context the read face (no tracking, queries must end in a Select() to a non-entity type, saving throws); AutoIncludeNavigationsConcern makes it the write face (every mapped navigation is eager-loaded, so a load always brings the whole aggregate).
  • IReadDbContextConcernBundle / IWriteDbContextConcernBundle — the standard set of each face as a single constructor dependency. Outside DI (design-time factories, tests) use ReadDbContextConcernBundle.ForDesignTime() / WriteDbContextConcernBundle.ForDesignTime().

Faces (Persistence.UnitOfWork, Persistence.Querying)

  • DbContextUnitOfWork<TDbContext> — base class for your IUnitOfWork; exposes repositories through Repository(...). SaveChangesAsync may be called once.
  • DbContextQuerySession<TDbContext> — base class for your IQuerySession; exposes repositories through QueryableRepository(...).
  • AddDbContextUnitOfWork<TService, TImplementation, TDbContext>() and AddDbContextQuerySession<TService, TImplementation, TDbContext>() — register a face, its IUnitOfWorkFactory<TService> / IQuerySessionFactory<TService>, a scoped IDbContextFactory and the EF Core IQueryableAsyncHelper. The shared registrations are added only once, so the two faces can be registered in either order, even over the same DbContext.
  • AddDbContextConcerns() — registers the concerns and the two bundles.

Everything else

  • MultiTenant.MultiTenantDbContext<TTenantEntity, TTenantId> — adds a TenantId shadow property, a foreign key to the tenant table and a global query filter to every entity, stamps new rows with the current tenant and rejects cross-tenant changes. DesignTimeTenantIdProvider<TTenantId> runs it in cross-tenant mode for dotnet ef. Cross-tenant contexts get a model without the filter, cached apart from the tenant-scoped one through its own IModelCacheKeyFactory, so both kinds can run in one process in any order; a derived context that replaces that factory must keep the mode in its key.
  • DatabaseMigration.DatabaseMigrator<TMarker, TDbContext> — implements IDatabaseMigrator<TMarker>: runs an IDatabasePreMigrationAction, then Database.MigrateAsync(). MigrationsAssemblyProvider<T> names the assembly that holds the migrations.
  • ValueObjects.SimpleValueObjectValueConverter<TValueObject, TWrapped> — persists a SimpleValueObject<T>, binding its constructor even when it is internal.
  • IDbDataSourceProvider / IDbContextOptionsAction — the seam a provider package fills in: the DbDataSource for a connection string, and how to configure DbContextOptionsBuilder with it.
  • Health.SimpleDbContextHealthChecker.CheckHealthAsync — a SELECT 1 test query for AddDbContextCheck.
  • Extensions.EntityTypeBuilderExtensions.HasIndex(string firstPropertyName, ...) — an index that starts with a shadow property such as TenantId.

Usage

dotnet add package Alvecta.Foundation.Infrastructure.EF

A read/write pair over one database (the ports IOrdersUnitOfWork and IOrdersQuerySession come from your *.Application.Ports project):

public class OrdersWriteDbContext(
    DbContextOptions<OrdersWriteDbContext> options,
    IWriteDbContextConcernBundle concerns)
    : ExtensibleDbContext(options, concerns.Concerns)
{
    public DbSet<Order> Orders => Set<Order>();
}

public class OrdersReadDbContext(
    DbContextOptions<OrdersReadDbContext> options,
    IReadDbContextConcernBundle concerns)
    : ExtensibleDbContext(options, concerns.Concerns)
{
    public DbSet<OrderView> Orders => Set<OrderView>();
}

public class OrdersUnitOfWork(IUnitOfWorkDependencyFactory<OrdersWriteDbContext> factory)
    : DbContextUnitOfWork<OrdersWriteDbContext>(factory), IOrdersUnitOfWork
{
    public IRepository<Order, OrderId> Orders => Repository(DbContext.Orders, x => x.OrderId);
}

public class OrdersQuerySession(IQuerySessionDependencyFactory<OrdersReadDbContext> factory)
    : DbContextQuerySession<OrdersReadDbContext>(factory), IOrdersQuerySession
{
    public IQueryableRepository<OrderView, OrderId> Orders => QueryableRepository(DbContext.Orders, x => x.OrderId);
}

Register them. The provider package supplies IDbDataSourceProvider and IDbContextOptionsAction:

services.AddDbContextConcerns();

services.AddDbContextUnitOfWork<IOrdersUnitOfWork, OrdersUnitOfWork, OrdersWriteDbContext>(SetDbContextOptions);
services.AddDbContextQuerySession<IOrdersQuerySession, OrdersQuerySession, OrdersReadDbContext>(SetDbContextOptions);

// The read context maps the full schema, so it owns the migrations.
services.AddTransient<IDatabaseMigrator<IOrdersUnitOfWork>, DatabaseMigrator<IOrdersUnitOfWork, OrdersReadDbContext>>();

static void SetDbContextOptions(IServiceProvider serviceProvider, DbContextOptionsBuilder builder)
{
    var connectionString = serviceProvider.GetRequiredService<OrdersDatabaseSettings>().ConnectionString;
    var dataSource = serviceProvider.GetRequiredService<IDbDataSourceProvider>().GetDataSource(connectionString);

    serviceProvider.GetRequiredService<IDbContextOptionsAction>().Invoke(builder, dataSource);
}

Then call QueryableConfiguration.UseQueryableAsyncExtensions from Alvecta.Foundation.Application.DI at startup. Alvecta.Foundation.Infrastructure.EF.Testing checks that the two models of a pair stay in step.

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 (3)

Showing the top 3 NuGet packages that depend on Alvecta.Foundation.Infrastructure.EF:

Package Downloads
Alvecta.Foundation.Infrastructure.EF.AuroraDsql

Package Description

Alvecta.Foundation.Infrastructure.EF.Postgres

Package Description

Alvecta.Foundation.Infrastructure.EF.Testing

EF Core persistence test harness for read/write DbContext pairs (scalar schema parity, migrations drift, pinned migration scripts, projection-only read-face guarantees) as abstract xunit base classes, plus an in-memory Sqlite database (SqliteTestDatabase) for integration tests over the real contexts. Add thin sealed run-host subclasses in your own test project.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
9.4.91 57 11/20/2026
9.3.90 127 11/13/2026
9.2.89 123 11/12/2026
9.1.88 125 11/11/2026
9.0.87 141 11/4/2026
9.0.86 126 11/4/2026
9.0.85 125 11/4/2026
9.0.84 121 11/4/2026
9.0.83 133 10/29/2026
9.0.82 130 10/29/2026
8.1.81 128 10/29/2026
8.0.80 123 10/28/2026
8.0.79 122 10/28/2026
8.0.78 132 10/28/2026
8.0.77 131 10/28/2026
7.1.76 126 10/27/2026
7.1.75 128 10/26/2026
7.0.74 123 10/26/2026
7.0.73 127 10/23/2026
7.0.72 127 10/23/2026
Loading failed