multi-tenancy

Implement tenant isolation in .NET with EF Core query filters and middleware.

4|1|Updated Mar 15, 2026
One-click install
npx skills add https://github.com/FaysilAlshareef/dotnet-ai-kit --skill multi-tenancy-faysilalshareef
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: multi-tenancy
Source: https://github.com/FaysilAlshareef/dotnet-ai-kit/tree/main/skills/architecture/multi-tenancy
Command: npx skills add https://github.com/FaysilAlshareef/dotnet-ai-kit --skill multi-tenancy-faysilalshareef

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Multi-tenant .NET applications risk data leakage, operational interference, and complex connection management; this guide provides patterns to enforce tenant isolation, automate onboarding, and prevent cross-tenant access.

Core Features & Use Cases

  • Tenant resolution middleware that resolves tenant context from JWT claims, headers, subdomain, or query parameters and makes it available via a scoped provider.
  • Per-tenant data strategies including database-per-tenant, schema-per-tenant, and shared-database with global EF Core query filters and tenant stamping on save.
  • Operational guidance for onboarding/offboarding tenants, caching with tenant keys, background job tenancy, and migration/backfill strategies for existing schemas.
  • Use Case: Convert a single-tenant order management system into a secure SaaS platform where each customer has logically isolated data and per-tenant connection handling.

Quick Start

Add tenant resolution middleware, register a scoped tenant provider, apply global EF Core query filters for all tenant-scoped entities, and route DbContext construction to per-tenant connection strings or fallback defaults.

Frequently Asked Questions about multi-tenancy

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement tenant isolation in a .NET SaaS application using EF Core?

Implement tenant isolation in .NET by applying EF Core global query filters to tenant-scoped entities and stamping tenant IDs on save. Use tenant resolution middleware to capture context and a scoped ITenantProvider to enforce data access boundaries.

What is the difference between database-per-tenant and schema-per-tenant strategies?

Database-per-tenant provides physical isolation by using separate databases per tenant, while schema-per-tenant isolates data within separate schemas of a shared database. Both strategies manage per-tenant connection routing differently within your .NET DbContext.

How do I resolve tenant context from JWT claims in ASP.NET Core middleware?

Resolve tenant context from JWT claims using ASP.NET Core middleware that extracts tenant identifiers early in the request pipeline. The middleware populates a scoped ITenantProvider, making the tenant context available for downstream dependency injection.

Can I use shared-database multi-tenancy with global EF Core query filters?

Yes, shared-database multi-tenancy uses global EF Core query filters to automatically restrict entity queries by tenant ID. This approach prevents cross-tenant data leakage without requiring separate databases or schemas for each tenant.

How do I automate tenant onboarding and database migrations in a multi-tenant .NET app?

Automate tenant onboarding in .NET by applying migration and backfill strategies that provision tenant-specific databases or schemas. Route DbContext construction to per-tenant connection strings to execute schema updates during the onboarding process.

When should I not use a shared-database tenancy strategy for my SaaS platform?

Avoid shared-database tenancy when tenants require strict physical data isolation or custom schema migrations. Database-per-tenant or schema-per-tenant strategies better serve these needs by preventing operational interference and simplifying per-tenant connection management.