TenantConnectionStringOptions<TKey> class
Namespace: Tenantry · Package: Tenantry.Core · API reference
How to find each tenant's connection string, for applications that give tenants their own database (or route them to different servers). Configure it with UseConnectionStrings and read connection strings through ITenantConnectionStringProvider<TKey>, or the current tenant's through CurrentTenantConnectionString<TKey>.
Set at least one delegate. ITenantConnectionStringProvider<TKey>.GetAsync prefers TenantConnectionStringOptions<TKey>.GetConnectionStringAsync and falls back to TenantConnectionStringOptions<TKey>.GetConnectionString; the synchronous Get needs TenantConnectionStringOptions<TKey>.GetConnectionString. The default provider does not cache.
public sealed class TenantConnectionStringOptions<TKey> where TKey : IEquatable<TKey>, IParsable<TKey>Type parameters
TKey: The tenant identifier type. SeeITenantDescriptor<TKey>for constraints.
Properties
GetConnectionString
Returns the connection string for a tenant. It runs whenever a connection string is read (for example each time a DbContext is created), so it should be quick: build the string from the tenant's properties rather than calling another service.
public Func<ITenantDescriptor<TKey>, string>? GetConnectionString { get; set; }Value: Func<ITenantDescriptor<TKey>, string>
options.GetConnectionString = tenant => $"Server=db;Database=app_{tenant.TenantId};Integrated Security=true";GetConnectionStringAsync
Returns the connection string for a tenant asynchronously, for connection strings held elsewhere such as a secrets vault. Only the asynchronous GetAsync methods can use it.
public Func<ITenantDescriptor<TKey>, CancellationToken, ValueTask<string>>? GetConnectionStringAsync { get; set; }Value: Func<ITenantDescriptor<TKey>, CancellationToken, ValueTask<string>>
ITenantStoreCache<TKey> interface
Removes cached tenants, so the next lookup asks the tenant store again.
TenantConnectionStringProvider<TKey> class
The default ITenantConnectionStringProvider<TKey>tenantry-itenantconnectionstringprovider.md: calls the configured delegates on every call, without caching.