ProBuilderEfCoreExtensions class
Namespace: Tenantry.Pro.EfCore · Package: Tenantry.Pro.EfCore · API reference
Extension methods for ProBuilder<TKey> that wire up EF Core services.
public static class ProBuilderEfCoreExtensionsMethods
AddDatabaseProvisioning<TKey, TService>(ProBuilder<TKey>)
Registers TService as the database-per-tenant provisioner singleton. Call this after pro.UseDatabasePerTenant(...) when you need on-demand database provisioning for new tenants.
public static ProBuilder<TKey> AddDatabaseProvisioning<TKey, TService>(this ProBuilder<TKey> builder) where TKey : IEquatable<TKey>, IParsable<TKey> where TService : class, ITenantInfrastructureProvisioner<TKey>Type parameters:
TKey: The tenant identifier type.TService: The provider-specificDatabaseProvisioningService<TKey>to register.
Parameters:
builderProBuilder<TKey>: The Pro builder.
Returns: ProBuilder<TKey>
AddSchemaPerTenantCaching<TKey>(ProBuilder<TKey>)
Registers EF Core model caching services for schema-per-tenant isolation.
public static ProBuilder<TKey> AddSchemaPerTenantCaching<TKey>(this ProBuilder<TKey> builder) where TKey : IEquatable<TKey>, IParsable<TKey>Type parameters:
TKey: The tenant identifier type. Must match the key type used inAddTenantry.
Parameters:
builderProBuilder<TKey>: The Pro builder obtained fromtenant.UsePro(...).
Returns: ProBuilder<TKey>: The same builder for chaining.
Call this after pro.UseSchemaPerTenant(...). Then, in your AddDbContext factory, call options.AddSchemaPerTenantCaching(sp) on the DbContextOptionsBuilder.
AddSchemaProvisioning<TKey, TService>(ProBuilder<TKey>, Action<SchemaProvisioningOptions>)
Registers TService as the schema-per-tenant provisioner singleton. Call this after pro.UseSchemaPerTenant(...) when you need on-demand schema provisioning for new tenants.
public static ProBuilder<TKey> AddSchemaProvisioning<TKey, TService>(this ProBuilder<TKey> builder, Action<SchemaProvisioningOptions> configure) where TKey : IEquatable<TKey>, IParsable<TKey> where TService : class, ITenantInfrastructureProvisioner<TKey>Type parameters:
TKey: The tenant identifier type.TService: The provider-specificSchemaProvisioningService<TKey>to register.
Parameters:
builderProBuilder<TKey>: The Pro builder.configureAction<SchemaProvisioningOptions>: Configures provisioning options. At minimum setSchemaProvisioningOptions.ConnectionStringto the shared database connection string.
Returns: ProBuilder<TKey>
WithMigrationOrchestration<TKey, TContext>(ProBuilder<TKey>, Func<string, TContext>, bool, bool)
Registers EF Core migration orchestration for database-per-tenant databases. Requires pro.UseDatabasePerTenant(...) to be called first.
[RequiresUnreferencedCode("EF Core migrations use reflection and are not trim-safe.")]
[RequiresDynamicCode("EF Core migrations generate code at run time and are not Native AOT-compatible.")]
public static ProBuilder<TKey> WithMigrationOrchestration<TKey, TContext>(this ProBuilder<TKey> builder, Func<string, TContext> contextFactory, bool runAtStartup = false, bool failStartupOnMigrationError = false) where TKey : IEquatable<TKey>, IParsable<TKey> where TContext : DbContextType parameters:
TKey: The tenant identifier type.TContext: The consumer's EF CoreDbContexttype.
Parameters:
builderProBuilder<TKey>: The Pro builder.contextFactoryFunc<string, TContext>: A factory that accepts a per-tenant connection string and returns a configuredTContext. The returned context is disposed after each migration run.runAtStartupbool: When true, registers a hosted service that migrates all tenant databases automatically before the application begins accepting requests. Every instance of the application does this, so with more than one instance prefer running migrations once per deployment instead (see the "Multiple instances" section of the migration orchestration guide).failStartupOnMigrationErrorbool: WithrunAtStartup, stop the application from starting when any tenant's migration fails, instead of logging the failures and serving traffic against the tenants whose schema is out of date. RequiresrunAtStartup.
Returns: ProBuilder<TKey>
Exceptions:
InvalidOperationException: Thrown immediately ifUseDatabasePerTenanthas not been called first.ArgumentException:failStartupOnMigrationErroris set withoutrunAtStartup.