TenantConnectionStringOptions<TKey> class
Namespace: Tenantry.Core · 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 ITenantConnectionStringResolver<TKey>.
Set at least one delegate. ITenantConnectionStringResolver<TKey>.ResolveAsync prefers TenantConnectionStringOptions<TKey>.GetConnectionStringAsync and falls back to TenantConnectionStringOptions<TKey>.GetConnectionString; the synchronous Resolve needs TenantConnectionStringOptions<TKey>.GetConnectionString. The resolver 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 resolved (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 ResolveAsync methods can use it.
public Func<ITenantDescriptor<TKey>, CancellationToken, ValueTask<string>>? GetConnectionStringAsync { get; set; }Value: Func<ITenantDescriptor<TKey>, CancellationToken, ValueTask<string>>
MissingTenantBehavior enum
Policy for what happens when a tenant-scoped operation runs without a resolved tenant context.
TenantConnectionStringResolver<TKey> class
The default ITenantConnectionStringResolver<TKey>tenantry-core-itenantconnectionstringresolver.md: calls the configured delegates on every resolution, without caching.