The three levels of tenancy that Marten supports are expressed in the enum TenancyStyle
with effective values of:
Single
, no multi-tenancyConjoined
, multi-tenancy through tenant idSeparate
, multi-tenancy through separate databases or schemas
Tenancy can be configured at the store level, applying to all documents or, at the most fine-grained level, on individual documents.
Tenancy Through Policies
Tenancy can be configured through Document Policies, accesible via StoreOptions.Policies
. The following sample demonstrates setting the default tenancy to TenancyStyle.Conjoined
for all documents.
storeOptions.Policies.AllDocumentsAreMultiTenanted();
// Shorthand for
// storeOptions.Policies.ForAllDocuments(_ => _.TenancyStyle = TenancyStyle.Conjoined);
Tenancy At Document Level & Policy Overrides
Tenancy can be configured at a document level through document mappings. This also enables overriding store-level configurations applied through Document Policies. The following sample demonstrates setting, through StoreOptions
the tenancy for Target
to TenancyStyle.Conjoined
, making it deviate from the configured default policy of TenancyStyle.Single
.
storeOptions.Policies.ForAllDocuments(x => x.TenancyStyle = TenancyStyle.Single);
storeOptions.Schema.For<Target>().MultiTenanted();