Skip to content

Pragmatic.MultiTenancy

Shared-schema multi-tenancy for the Pragmatic.Design ecosystem: tenant resolution, scoping, and automatic query filtering — zero config for single-tenant apps, pluggable strategies for SaaS.

Multi-tenant SaaS must isolate data per tenant. Without framework support, every query must filter by tenant, every write must assign it, and every background job must propagate the tenant context. Miss one WHERE TenantId = @current and you have a cross-tenant data leak — something code review catches sometimes and tests catch never.

// Without Pragmatic: manual tenant filtering everywhere (easy to forget → data leak)
return await db.Invoices.Where(i => i.TenantId == currentTenantId).ToListAsync(ct);

Caching leaks too (invoices:latest returns the wrong tenant’s data), and resolution varies (headers in dev, JWT in prod, subdomains for some APIs).

Mark entities ITenantEntity; the generator filters and assigns the tenant automatically, and an AsyncLocal tenant context flows through async continuations (including background jobs).

[Entity<Guid>]
public partial class Invoice : IEntity<Guid>, ITenantEntity { /* TenantId set + filtered automatically */ }
// resolution strategy chosen once, in Program.cs
app.UseMultiTenancy(mt => mt.UseHeader()); // or claim / subdomain / route / custom; .UseDbPerTenant() for DB-per-tenant
Terminal window
dotnet add package Pragmatic.MultiTenancy

Tenant resolution strategies, shared-schema filtering, DB-per-tenant, and non-HTTP propagation are functional within the 0.8 preview. See the roadmap.

| Concepts | The tenancy model, ITenantEntity, ambient context, cache scoping | | Getting Started | Mark entities, choose a resolver, wire the middleware | | Tenant Resolution | Header/claim/subdomain/route/custom resolvers, ordering, non-HTTP contexts | | Common Mistakes | The most frequent tenancy pitfalls | | Troubleshooting | Problem/solution guide |

  • .NET 10.0+

Part of the Pragmatic.Design ecosystem — see Licensing. Pragmatic.MultiTenancy is licensed under the PolyForm Small Business 1.0.0 license (free for small businesses; commercial license above the threshold).