|
2 | 2 |
|
3 | 3 | ## Multi-Tenancy |
4 | 4 |
|
5 | | -### Conjoined Tenancy (Shared Database) |
6 | | - |
7 | | -All tenants share the same PostgreSQL database. Data is isolated by a `tenant_id` column on every table. |
8 | | - |
9 | | -```csharp |
10 | | -// Configuration |
11 | | -options.Events.TenancyStyle = TenancyStyle.Conjoined; |
12 | | -options.Policies.AllDocumentsAreMultiTenanted(); |
13 | | -``` |
14 | | - |
15 | | -Every query and event append automatically filters/tags with the current tenant ID — no manual filtering needed. |
16 | | - |
17 | | -### Per-Tenant Sessions |
18 | | - |
19 | | -Sessions must be scoped to a specific tenant. Wolverine creates tenant-appropriate sessions automatically when the tenant is part of the `IMessageContext`. For manual control: |
20 | | - |
21 | | -```csharp |
22 | | -// Open a session for a specific tenant |
23 | | -using var session = store.LightweightSession("acme"); // by tenant ID string |
24 | | -// or |
25 | | -using var session = store.LightweightSession(tenantId); |
26 | | - |
27 | | -// Query session |
28 | | -using var query = store.QuerySession("acme"); |
29 | | -``` |
30 | | - |
31 | | -### Global (Non-Tenanted) Documents |
32 | | - |
33 | | -Some documents should be accessible across all tenants (e.g., tenant registry, global config): |
34 | | - |
35 | | -```csharp |
36 | | -[Marten.Schema.DoNotPartition] |
37 | | -public class Tenant |
38 | | -{ |
39 | | - public string Id { get; set; } = string.Empty; |
40 | | - public string Name { get; set; } = string.Empty; |
41 | | - public bool IsEnabled { get; set; } |
42 | | -} |
43 | | -``` |
44 | | - |
45 | | -> `[DoNotPartition]` requires Marten 8.5+. Without it, documents get a `tenant_id` column and cannot be queried globally. |
46 | | -
|
47 | | -### Conjoined vs Separate Databases |
48 | | - |
49 | | -| Strategy | Isolation | Complexity | Use Case | |
50 | | -|----------|-----------|------------|----------| |
51 | | -| **Conjoined** | Row-level (`tenant_id`) | Low | Multi-tenant SaaS, shared infra | |
52 | | -| **Separate Schemas** | Schema-level | Medium | Stricter isolation requirements | |
53 | | -| **Separate Databases** | DB-level | High | Compliance, very high isolation | |
54 | | - |
55 | | -For most applications, conjoined tenancy is the right choice. |
| 5 | +See [marten-multi-tenancy.md](marten-multi-tenancy.md) for the full reference — conjoined tenancy config, per-tenant sessions, DI registration, middleware, global documents, cross-tenant queries, projection tenancy, table partitioning, index strategy, and tenant lifecycle management. |
56 | 6 |
|
57 | 7 | --- |
58 | 8 |
|
|
0 commit comments