Alvecta.Foundation.Infrastructure.EF
9.4.91
dotnet add package Alvecta.Foundation.Infrastructure.EF --version 9.4.91
NuGet\Install-Package Alvecta.Foundation.Infrastructure.EF -Version 9.4.91
<PackageReference Include="Alvecta.Foundation.Infrastructure.EF" Version="9.4.91" />
<PackageVersion Include="Alvecta.Foundation.Infrastructure.EF" Version="9.4.91" />
<PackageReference Include="Alvecta.Foundation.Infrastructure.EF" />
paket add Alvecta.Foundation.Infrastructure.EF --version 9.4.91
#r "nuget: Alvecta.Foundation.Infrastructure.EF, 9.4.91"
#:package Alvecta.Foundation.Infrastructure.EF@9.4.91
#addin nuget:?package=Alvecta.Foundation.Infrastructure.EF&version=9.4.91
#tool nuget:?package=Alvecta.Foundation.Infrastructure.EF&version=9.4.91
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— aDbContextthat runs a list ofIDbContextConcerns at each lifecycle stage (OnConfiguring,ConfigureConventions,OnModelCreating,OnSavingChangesAsync). SynchronousSaveChangesthrows.- The standard concerns:
ConcurrencyStampConcern(aConcurrencyStampshadow concurrency token on every entity, renewed on save),DateTimeOffsetUtcConcern(stores everyDateTimeOffsetas UTC),RequiredFieldValidationConcern(fails a save that leaves a required propertynull) andNoActionDeleteBehaviorConcern(foreign keys default toNoActioninstead of cascading; owned types and an explicitOnDelete()are left alone). - The face concerns:
ProjectionOnlyQueryConcernmakes a context the read face (no tracking, queries must end in aSelect()to a non-entity type, saving throws);AutoIncludeNavigationsConcernmakes 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) useReadDbContextConcernBundle.ForDesignTime()/WriteDbContextConcernBundle.ForDesignTime().
Faces (Persistence.UnitOfWork, Persistence.Querying)
DbContextUnitOfWork<TDbContext>— base class for yourIUnitOfWork; exposes repositories throughRepository(...).SaveChangesAsyncmay be called once.DbContextQuerySession<TDbContext>— base class for yourIQuerySession; exposes repositories throughQueryableRepository(...).AddDbContextUnitOfWork<TService, TImplementation, TDbContext>()andAddDbContextQuerySession<TService, TImplementation, TDbContext>()— register a face, itsIUnitOfWorkFactory<TService>/IQuerySessionFactory<TService>, a scopedIDbContextFactoryand the EF CoreIQueryableAsyncHelper. The shared registrations are added only once, so the two faces can be registered in either order, even over the sameDbContext.AddDbContextConcerns()— registers the concerns and the two bundles.
Everything else
MultiTenant.MultiTenantDbContext<TTenantId>— adds aTenantIdshadow property and a global query filter to every entity, stamps new rows with the current tenant and rejects cross-tenant changes. It maps no tenant entity and adds no foreign key, so it serves a database that does not hold the tenants (they live in another schema and are referred to by id). Without a foreign key EF Core adds no index onTenantIdeither: declare the tenant-leading indexes the queries need.MultiTenant.MultiTenantDbContext<TTenantEntity, TTenantId>builds on it for the database that holds the tenants: every row also gets a foreign key to the tenant table, and the tenant entity is left unscoped and cannot be changed once stored.DesignTimeTenantIdProvider<TTenantId>runs either in cross-tenant mode fordotnet ef. Cross-tenant contexts get a model without the filter, cached apart from the tenant-scoped one through its ownIModelCacheKeyFactory, 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>— implementsIDatabaseMigrator<TMarker>: runs anIDatabasePreMigrationAction, thenDatabase.MigrateAsync().MigrationsAssemblyProvider<T>names the assembly that holds the migrations.ValueObjects.SimpleValueObjectValueConverter<TValueObject, TWrapped>— persists aSimpleValueObject<T>, binding its constructor even when it isinternal.IDbDataSourceProvider/IDbContextOptionsAction— the seam a provider package fills in: theDbDataSourcefor a connection string, and how to configureDbContextOptionsBuilderwith it.Health.SimpleDbContextHealthChecker.CheckHealthAsync— aSELECT 1test query forAddDbContextCheck.Extensions.EntityTypeBuilderExtensions.HasIndex(string firstPropertyName, ...)— an index that starts with a shadow property such asTenantId.
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 | 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
- Alvecta.Foundation.Application.Ports (>= 9.4.91)
- Alvecta.Foundation.Domain.ValueObjects (>= 9.4.91)
- Alvecta.Utils (>= 8.6.85)
- Alvecta.Utils.DependencyInjection (>= 8.6.85)
- Alvecta.Utils.Queryable (>= 8.6.85)
- Alvecta.Utils.Queryable.EF (>= 8.6.85)
- Microsoft.EntityFrameworkCore (>= 10.0.15)
- Microsoft.EntityFrameworkCore.Relational (>= 10.0.15)
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 |