I am creating a Multi Tenant webapi using SaaSKit, I am able to configure everything exactly as I wanted, however I need to run the commands in the Package Manager Console, the main one of them would be to add the migrations in the database used for each Tenant, since this database Data is created at runtime, I have a parameter of type AppTenant in the DbContext constructor, there comes the problem when I want to add the target migration to this database, the console displays the error:
Unable to resolve service for type 'xxx.xxx.xxx.AppTenant' while attempting to activate 'xxx.xxx.xxx.AppTenantContext'.
I do not know if I am doing everything right, but I believe the solution would be to create a class that implements an IDbContextFactory, but in the Multi Tenancy application form we are using, the database selected for each Tenant is defined by a resolver that selects The ideal database for the tenant according to the host accessed, ie the only solution I see possible would be to create a default Tenant with an IDbContextFactory only to run this Add-Migration command.
But then, how do I create a factory that creates this default instance in the builder of my DbContext? Is this the best solution?
PS: I already tested it by creating another empty constructor or with the configurations
AppTenantContext constructor:
private readonly AppTenant _tenant;
public AppTenantContext(AppTenant tenant)
{
if (tenant != null)
{
_tenant = tenant;
Database.EnsureCreated();
if (Database.GetPendingMigrations().Count() > 0)
Database.Migrate();
}
}