using ApiPolo.Data; using ApiPolo.Interfaces; using static ApiPolo.Controllers.PoloController; public class TenantDbContextFactory : ITenantDbContextFactory { private readonly IServiceProvider _serviceProvider; public TenantDbContextFactory(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } public ITenantDbContext GetDbContext(string tenant) { // Create a scope to resolve DbContext var scope = _serviceProvider.CreateScope(); return tenant switch { Clienti.Maras => scope.ServiceProvider.GetRequiredService(), Clienti.Marrocco => scope.ServiceProvider.GetRequiredService(), // Add other tenants as needed // Clienti.AnotherTenant => scope.ServiceProvider.GetRequiredService(), _ => throw new KeyNotFoundException($"No DbContext found for tenant: {tenant}") }; } }