Docs
Tenantry ProAPI reference

DatabasePerTenantOptions<TKey> class

Namespace: Tenantry.Pro.Strategies.DatabasePerTenant · Package: Tenantry.Pro · API reference

Options for the database-per-tenant strategy. Configure via pro.UseDatabasePerTenant(opts => { ... }).

public sealed class DatabasePerTenantOptions<TKey> where TKey : IEquatable<TKey>, IParsable<TKey>

Type parameters

  • TKey: The tenant identifier type.

Properties

CacheConnectionStrings

When true, resolved connection strings are cached in memory for DatabasePerTenantOptions<TKey>.CacheDuration before being re-resolved. Enable this when DatabasePerTenantOptions<TKey>.GetConnectionStringAsync performs expensive lookups.

public bool CacheConnectionStrings { get; set; }

Value: bool

At-rest DatabasePerTenantOptions<TKey>.Encryption (including UseDataProtectionEncryption()) only applies to cached values, so it has no effect unless caching is enabled.

CacheDuration

How long a cached connection string is considered valid. Defaults to 30 minutes. Only used when DatabasePerTenantOptions<TKey>.CacheConnectionStrings is true.

public TimeSpan CacheDuration { get; set; }

Value: TimeSpan

Encryption

Configures at-rest encryption for cached connection strings. When set to any mode other than ConnectionStringEncryptionMode.None, the cache stores ciphertext and decrypts on every cache hit. Only effective when DatabasePerTenantOptions<TKey>.CacheConnectionStrings is true.

public ConnectionStringEncryptionOptions Encryption { get; set; }

Value: ConnectionStringEncryptionOptions

GetConnectionString

A delegate that returns the connection string for the given tenant descriptor. Registered with Core's TenantConnectionStringOptions<TKey>; read connection strings through ITenantConnectionStringResolver<TKey>, which also applies caching when enabled. Without caching it runs every time a DbContext is created, so it must be fast — compute the string from tenant properties, do not call external services.

public Func<ITenantDescriptor<TKey>, string>? GetConnectionString { get; set; }

Value: Func<ITenantDescriptor<TKey>, string>

opts.GetConnectionString = tenant => $"Server=.;Database=myapp_{tenant.TenantId};Integrated Security=true";

GetConnectionStringAsync

An asynchronous delegate that returns the connection string for the given tenant descriptor. Use this when the connection string must be retrieved from an external source (e.g. a secrets vault or a configuration database). If both DatabasePerTenantOptions<TKey>.GetConnectionString and DatabasePerTenantOptions<TKey>.GetConnectionStringAsync are set, the async delegate is preferred by ITenantConnectionStringResolver.ResolveAsync.

public Func<ITenantDescriptor<TKey>, CancellationToken, ValueTask<string>>? GetConnectionStringAsync { get; set; }

Value: Func<ITenantDescriptor<TKey>, CancellationToken, ValueTask<string>>

opts.GetConnectionStringAsync = async (tenant, ct) =>
    await secretsClient.GetSecretAsync($"connstr-{tenant.TenantId}", ct);

On this page