dbcontext con tabelle dinamiche

This commit is contained in:
LORENZO\pacio 2025-03-26 14:40:49 +01:00
parent c5dfa2ef58
commit a9e2888d07
2 changed files with 8 additions and 9 deletions

View File

@ -58,7 +58,6 @@ namespace ApiPolo.Controllers
private readonly Service _service;
private readonly TokenDbContext? _tokens;
private readonly Maras_DbContext _Maras;
/*
#region TECNICI
@ -532,8 +531,7 @@ namespace ApiPolo.Controllers
ConfigurazioniDbContext config,
ITenantDbContextFactory dbContextFactory,
Service service,
TokenDbContext tokens,
Maras_DbContext maras
TokenDbContext tokens
/*
#region tecnici
@ -905,7 +903,6 @@ namespace ApiPolo.Controllers
_service = service;
_config = config;
_tokens = tokens;
_Maras = maras;
/*
#region TECNICI

View File

@ -8,6 +8,7 @@ namespace ApiPolo.Data
public class TenantDbContext : DbContext,ITenantDbContext
{
private readonly string _connectionString;
private readonly string _tenantCode;
#region PROPERTIES
public DbSet<Caus_Rapp>? Causali { get; set; }
@ -66,6 +67,7 @@ namespace ApiPolo.Data
: base(options)
{
// Fetch the connection string dynamically based on tenant
_tenantCode = tenant;
_connectionString = configuration.GetConnectionString(tenant)
?? throw new KeyNotFoundException($"No connection string found for tenant: {tenant}");
}
@ -126,14 +128,14 @@ namespace ApiPolo.Data
TryConfigureEntity(mb =>
{
mb.Entity<Compo_Impia_Table>().ToTable("DISCOMICOMIMP");
mb.Entity<Compo_Impia_Table>().ToTable($"{_tenantCode}MICOMIMP");
mb.Entity<Compo_Impia_Table>().HasKey(table => new
{
table.cocodimp,
table.cprownum,
table.cocodazi
});
}, "DISCOMICOMIMP");
}, $"{_tenantCode}MICOMIMP");
TryConfigureEntity(mb => mb.Entity<Compo_Impia>().ToView("API_MICOMIMP"), "API_MICOMIMP");
@ -141,15 +143,15 @@ namespace ApiPolo.Data
TryConfigureEntity(mb =>
{
mb.Entity<Mag_New>().ToTable("DISCOMAG_NEW");
mb.Entity<Mag_New>().ToTable($"{_tenantCode}MAG_NEW");
mb.Entity<Mag_New>().HasKey(table => new
{
table.seriale_rapportino,
table.riga
});
}, "DISCOMAG_NEW");
}, $"{_tenantCode}MAG_NEW");
TryConfigureEntity(mb => mb.Entity<Magazzini>().ToTable("DISCOMAGAZZIN"), "DISCOMAGAZZIN");
TryConfigureEntity(mb =>mb.Entity<Magazzini>().ToTable($"{_tenantCode}MAGAZZIN"), $"{_tenantCode}MAGAZZIN");
TryConfigureEntity(mb => mb.Entity<Manprog>().ToView("API_MANPROG"), "API_MANPROG");