ApiManutenzioni/ApiPolo/Interfaces/TenantDbContextFactory.cs
2025-03-24 14:00:32 +01:00

30 lines
1.0 KiB
C#

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
using (var scope = _serviceProvider.CreateScope())
{
return tenant switch
{
Clienti.Maras => scope.ServiceProvider.GetRequiredService<Maras_DbContext>(),
Clienti.Marrocco => scope.ServiceProvider.GetRequiredService<Marro_DbContext>(),
// Add other tenants as needed
// Clienti.AnotherTenant => scope.ServiceProvider.GetRequiredService<AnotherTenant_DbContext>(),
_ => throw new KeyNotFoundException($"No DbContext found for tenant: {tenant}")
};
}
}
}