202 lines
6.9 KiB
C#
202 lines
6.9 KiB
C#
using ApiPolo.Interfaces;
|
|
using ApiPolo.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
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; }
|
|
|
|
public DbSet<Chiusure>? Chiusure { get; set; }
|
|
|
|
public DbSet<AziendaRif>? Azi { get; set; }
|
|
|
|
public DbSet<CC_CI>? ccci { get; set; }
|
|
|
|
public DbSet<CC_CIWiev>? ccciWiev { get; set; }
|
|
|
|
public DbSet<Chiamate>? Chiamate { get; set; }
|
|
|
|
public DbSet<Anag>? Clienti { get; set; }
|
|
|
|
public DbSet<Compo_Impia_Table>? Componen { get; set; } // IN CONFLITTO CON Compo_Impia
|
|
|
|
//public DbSet<Compo_Impia>? Componen { get; set; } //CAMBIARE NOME!!!
|
|
|
|
public DbSet<Impianto>? Impia { get; set; }
|
|
|
|
public DbSet<Mag_New>? Mag { get; set; }
|
|
|
|
public DbSet<Magazzini>? Magaz { get; set; }
|
|
|
|
public DbSet<Manprog>? Manutenzioni { get; set; }
|
|
|
|
public DbSet<Pagam>? Pagamenti { get; set; }
|
|
|
|
public DbSet<Prese>? Prese { get; set; }
|
|
|
|
public DbSet<Rapp_New>? Rapps { get; set; }
|
|
|
|
public DbSet<Rappmast>? rapp { get; set; }
|
|
|
|
public DbSet<Sostituzione>? Sost { get; set; }
|
|
|
|
public DbSet<Saldiart>? Saldi { get; set; }
|
|
|
|
public DbSet<Sto_Rapp>? StoRapp { get; set; }
|
|
|
|
public DbSet<Tecnici>? Tecnici { get; set; }
|
|
public DbSet<Sto_Imp>? StoImp { get; set; }
|
|
|
|
public DbSet<Timbratura>? Timbr { get; set; }
|
|
|
|
public DbSet<Commessa>? Commesse { get; set; }
|
|
|
|
public DbSet<Prog2>? Prog { get; set; }
|
|
//public DbSet<Prog>? Progressivi { get; set; }
|
|
//public DbSet<Compo_Impia>? Compo { get; set; }
|
|
#endregion
|
|
|
|
public TenantDbContext(DbContextOptions<TenantDbContext> options, IConfiguration configuration, string tenant)
|
|
: 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}");
|
|
}
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
optionsBuilder.UseSqlServer(_connectionString);
|
|
}
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
void TryConfigureEntity(Action<ModelBuilder> configure, string entityName)
|
|
{
|
|
try
|
|
{
|
|
configure(modelBuilder);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Warning: Skipping entity '{entityName}' due to error: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Caus_Rapp>().ToView("API_CAUS_RAPP"), "API_CAUS_RAPP");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Chiusure>().ToView("API_CHIUSURE"), "API_CHIUSURE");
|
|
|
|
TryConfigureEntity(mb =>
|
|
{
|
|
mb.Entity<AziendaRif>().ToTable("PIAZIRIF");
|
|
mb.Entity<AziendaRif>().HasKey(table => new
|
|
{
|
|
table.piazihoc,
|
|
table.picodtec,
|
|
table.pirifazi
|
|
});
|
|
}, "PIAZIRIF");
|
|
|
|
TryConfigureEntity(mb =>
|
|
{
|
|
mb.Entity<CC_CI>().ToTable("PIASSCHIU");
|
|
mb.Entity<CC_CI>().HasKey(table => new
|
|
{
|
|
table.picodazi,
|
|
table.picodint,
|
|
table.picodchi
|
|
});
|
|
}, "PIASSCHIU");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<CC_CIWiev>().ToView("API_ASSCHIU"), "API_ASSCHIU");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Chiamate>().ToView("API_CHIAMATE"), "API_CHIAMATE");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Anag>().ToView("API_CLIENTI"), "API_CLIENTI");
|
|
|
|
TryConfigureEntity(mb =>
|
|
{
|
|
mb.Entity<Compo_Impia_Table>().ToTable($"{_tenantCode}MICOMIMP");
|
|
mb.Entity<Compo_Impia_Table>().HasKey(table => new
|
|
{
|
|
table.cocodimp,
|
|
table.cprownum,
|
|
table.cocodazi
|
|
});
|
|
}, $"{_tenantCode}MICOMIMP");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Compo_Impia>().ToView("API_MICOMIMP"), "API_MICOMIMP");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Impianto>().ToView("API_IMPIANTI"), "API_IMPIANTI");
|
|
|
|
TryConfigureEntity(mb =>
|
|
{
|
|
mb.Entity<Mag_New>().ToTable($"{_tenantCode}MAG_NEW");
|
|
mb.Entity<Mag_New>().HasKey(table => new
|
|
{
|
|
table.seriale_rapportino,
|
|
table.riga
|
|
});
|
|
}, $"{_tenantCode}MAG_NEW");
|
|
|
|
TryConfigureEntity(mb =>mb.Entity<Magazzini>().ToTable($"{_tenantCode}MAGAZZIN"), $"{_tenantCode}MAGAZZIN");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Manprog>().ToView("API_MANPROG"), "API_MANPROG");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Pagam>().ToView("API_PAGAMENTI"), "API_PAGAMENTI");
|
|
|
|
TryConfigureEntity(mb =>
|
|
{
|
|
mb.Entity<Prese>().ToTable("PIPRESA");
|
|
mb.Entity<Prese>().HasKey(table => new
|
|
{
|
|
table.picodazi,
|
|
table.pimpianto,
|
|
table.picodint,
|
|
table.pidatman
|
|
});
|
|
}, "PIPRESA");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Rapp_New>().ToTable("RAPP_NEW"), "RAPP_NEW");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Rappmast>().ToView("API_RAPPORTINI"), "API_RAPPORTINI");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Saldiart>().ToView("API_SALDIART_PREZZI"), "API_SALDIART_PREZZI");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Sostituzione>().ToView("API_SOSTITUZIONI"), "API_SOSTITUZIONI");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Sto_Rapp>().ToView("API_STO_RAPP"), "API_STO_RAPP");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Tecnici>().ToView("API_TECNICI"), "API_TECNICI");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Timbratura>().ToTable("TIMBRATURE"), "TIMBRATURE");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Commessa>().ToView("API_COMMESSE"), "API_COMMESSE");
|
|
|
|
TryConfigureEntity(mb => mb.Entity<Sto_Imp>().ToView("API_STO_IMP"), "API_STO_IMP");
|
|
|
|
TryConfigureEntity(mb =>
|
|
{
|
|
mb.Entity<Prog2>().ToTable("CPWARN");
|
|
mb.Entity<Prog2>().HasKey(table => new
|
|
{
|
|
table.tablecode,
|
|
table.warncode
|
|
});
|
|
}, "CPWARN");
|
|
}
|
|
}
|
|
}
|