Overview
The multi-tenancy system provides:- Database-Per-Tenant: Complete data isolation with separate databases
- Tenant Resolution: Automatic tenant detection from HTTP headers, routes, or hosts
- Tenant Management: CRUD operations for tenant administration
- Migration Support: Automatic database migrations for new tenants
- Tenant Context: Access current tenant information throughout the application
Architecture
Finbuckle.MultiTenant Integration
FullStackHero uses Finbuckle.MultiTenant for multi-tenancy:Extensions.cs
AppTenantInfo
TheAppTenantInfo class stores tenant information:
Tenant Resolution
Tenants are identified using multiple strategies:1. HTTP Header Strategy
The most common approach is using thetenant HTTP header:
2. Route Strategy
Include the tenant in the URL path:3. Host Strategy
Use subdomains for tenant identification:The default configuration uses the HTTP header strategy with the
tenant header. This provides the most flexibility for API clients.Configuration
Configure multi-tenancy options inappsettings.json:
appsettings.json
Whether to automatically apply migrations to all tenant databases on application startup.
Database-Per-Tenant
Tenant Database Context
Each tenant has its own database context:TenantDbContext.cs
Accessing Tenant Context
Access the current tenant usingIMultiTenantContextAccessor:
Tenant Management
Creating a Tenant
Create a new tenant with its own database:Create Database
The system creates a new database for the tenant using the provided connection string.
Tenant Activation/Deactivation
Toggle tenant activation status:ChangeTenantActivationCommand.cs
PUT /api/v1/multitenancy/tenants/{id}/activation
Tenant Migrations
Automatic Migrations
WhenRunTenantMigrationsOnStartup is enabled, migrations are applied to all tenant databases on startup.
Manual Migration Management
Get the migration status for a tenant: Endpoint:GET /api/v1/multitenancy/tenants/{id}/migrations
Response
Health Checks
TheTenantMigrationsHealthCheck verifies tenant database health:
TenantMigrationsHealthCheck.cs
Tenant Isolation
Data Isolation
All data is completely isolated between tenants:- Each tenant has a separate database
- No shared tables or data
- Queries automatically filter by tenant context
User Isolation
Users belong to a single tenant:- User credentials are tenant-specific
- Authentication tokens include tenant ID in claims
- Cross-tenant access is not possible
Configuration Isolation
Tenants can have independent configuration:Working with Multi-Tenancy
Command Handlers
Command handlers automatically operate within the tenant context:Repositories
Repositories use the tenant-specific DbContext:Root Tenant
The “root” tenant is a special administrative tenant:- Manages other tenants
- Cannot be deleted or deactivated
- Has access to tenant management endpoints
appsettings.json
The root tenant uses the default connection string from
DatabaseOptions. All other tenants have their own connection strings.Best Practices
Use Connection String Encryption
Use Connection String Encryption
Encrypt tenant connection strings in the database. Never store them in plain text.
Implement Tenant Quotas
Implement Tenant Quotas
Set limits on storage, users, and API calls per tenant to prevent resource exhaustion.
Test Tenant Isolation
Test Tenant Isolation
Write integration tests to verify that data cannot leak between tenants.
Monitor Tenant Databases
Monitor Tenant Databases
Track database size, query performance, and connection pool usage per tenant.
Backup Tenant Databases Separately
Backup Tenant Databases Separately
Implement per-tenant backup and restore capabilities.
Tenant Resolution Flow
Related Topics
Authentication
Learn about tenant-aware authentication
Authorization
Understand tenant-isolated authorization
Observability
Monitor tenant-specific metrics and traces
Background Jobs
Run tenant-aware background jobs
