MultiTenant for EF Core

Per-tenant data isolation for EF Core, in a shared database, a database per tenant, or a mix of both.

MultiTenant Entity Framework Core keeps each tenant's data separate. Mark entities as multi-tenant and queries, inserts, and updates stay within the current tenant.

Use a shared database with automatic filtering, a database per tenant chosen from tenant info, or a hybrid of both. It also provides an EF Core tenant store.

dotnet add package Finbuckle.MultiTenant.EntityFrameworkCore
builder.Services.AddMultiTenant<AppTenantInfo, string>()
    .WithHostStrategy()
    .WithConfigurationStore();

builder.Services.AddMultiTenantDbContext<AppDbContext, string>(options =>
    options.UseSqlServer(connectionString));

Releases

Version Targets Released Release notes
v11.0.0New .NET 11 Nov 24, 2026 Release notes
v10.2.0 .NET 10 Nov 12, 2026 Release notes
v9.4.16 .NET 8 and 9 Nov 10, 2026 Release notes
v8.1.21 .NET 6, 7, and 8 Nov 10, 2026 Release notes

All versions on NuGet · All releases on GitHub

One database, isolated per tenant

Mark entities as multi-tenant and MultiTenant adds a tenant ID, filters every query by the current tenant, and stamps new rows automatically.

  • Use the [MultiTenant] attribute or the fluent API.
  • Derive from MultiTenantDbContext, or add MultiTenant to an existing DbContext.
  • Indexes and keys can include the tenant ID.
Read about shared databases
[MultiTenant]
public class Invoice
{
    public int Id { get; set; }
    public decimal Amount { get; set; }
}

public class AppDbContext(DbContextOptions<AppDbContext> options)
    : MultiTenantDbContext<string>(options)
{
    public DbSet<Invoice> Invoices => Set<Invoice>();
}

A database per tenant

Give each tenant its own database, or move only your largest tenants to dedicated ones, by choosing the connection from the tenant's details.

  • Pick the connection string or provider for each tenant.
  • Mix shared and dedicated databases in one app.
  • Pooled DbContext registration for high-throughput apps.
Read about separate databases
builder.Services.AddMultiTenantDbContext<AppDbContext, string>((sp, options) =>
{
    var tenant = sp
        .GetRequiredService<ITenantContext<AppTenantInfo, string>>()
        .TenantInfo;

    options.UseSqlServer(tenant?.ConnectionString);
});

Writes that can't cross tenants

Saving changes checks every tracked entity against the context's tenant, so data can't be written to the wrong tenant by mistake.

  • A tenant mismatch throws by default; ignore or overwrite if you prefer.
  • New entities saved without a tenant ID get the current one.
  • Query across tenants deliberately with IgnoreQueryFilters.
Read about adding and querying data
var invoice = new Invoice { Amount = 120m };
db.Invoices.Add(invoice);

// TenantId is set from the current tenant
await db.SaveChangesAsync();

// only the current tenant's invoices
var unpaid = await db.Invoices
    .Where(i => i.Amount > 0)
    .ToListAsync();

Store tenants in EF Core

Keep your tenant list in a database you already run, managed through EF Core migrations like the rest of your schema.

  • Works with any EF Core provider.
  • Combine it with a distributed cache to keep lookups fast.
  • Add, update, and remove tenants at runtime with TenantManager.
Read about the EF Core store
public class TenantStoreDbContext(DbContextOptions options)
    : EFCoreStoreDbContext<AppTenantInfo, string>(options);

builder.Services.AddMultiTenant<AppTenantInfo, string>()
    .WithHostStrategy()
    .WithDistributedCacheStoreCache()
    .WithEFCoreStore<TenantStoreDbContext, AppTenantInfo, string>();

The rest of the MultiTenant family

MultiTenant Abstractions

Finbuckle.MultiTenant.Abstractions

The shared contracts: tenant info, strategy, store, and tenant context interfaces, with no implementation dependencies.

Learn more

MultiTenant

Finbuckle.MultiTenant

The foundation: tenant resolution, tenant stores, per-tenant options, and running work as a tenant in any .NET app.

Learn more

MultiTenant for ASP.NET Core

Finbuckle.MultiTenant.AspNetCore

Middleware, request-based strategies, and per-tenant authentication for ASP.NET Core apps.

Learn more

MultiTenant Identity

Finbuckle.MultiTenant.Identity.EntityFrameworkCore

Multi-tenant ASP.NET Core Identity: each tenant gets its own users, roles, and passkeys.

Learn more