ApiManutenzioni/ApiPolo/Interfaces/TenantDbContextFactory.cs
LORENZO\pacio d25173b0c5 dbcontext per maras (come esempio futuri dbcontext)
implementazione interfaccie e factory per dbcontext universali
creato TestController per esempi
2025-03-22 12:52:40 +01:00

32 lines
1.0 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using ApiPolo.Interfaces;
using System;
using System.Collections.Generic;
using static ApiPolo.Controllers.PoloController;
using ApiPolo.Data;
namespace ApiPolo.Services
{
public class TenantDbContextFactory : ITenantDbContextFactory
{
private readonly IServiceProvider _serviceProvider;
public TenantDbContextFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public ITenantDbContext GetDbContext(string tenant)
{
return tenant switch
{
Clienti.Maras => _serviceProvider.GetRequiredService<Maras_DbContext>(),
// Add other tenants as needed
// Clienti.AnotherTenant => _serviceProvider.GetRequiredService<AnotherTenant_DbContext>(),
_ => throw new KeyNotFoundException($"No DbContext found for tenant: {tenant}")
};
}
}
}