implementazione interfaccie e factory per dbcontext universali creato TestController per esempi
32 lines
1.0 KiB
C#
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}")
|
|
};
|
|
}
|
|
}
|
|
}
|