dbcontext multitenant
This commit is contained in:
parent
48c195e477
commit
efa0cb9106
@ -6649,6 +6649,31 @@ namespace ApiPolo.Controllers
|
||||
return StatusCode(StatusCodes.Status200OK, mex);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("chiamate/test2")]
|
||||
public async Task<ActionResult<Chiamate_out>> Test2([FromBody] Rapp_New model)
|
||||
{
|
||||
var dbContext = _dbContextFactory.GetDbContext("MARAS");
|
||||
|
||||
string mex = string.Empty;
|
||||
try
|
||||
{
|
||||
using (var transaction = await dbContext.Database.BeginTransactionAsync())
|
||||
{
|
||||
await dbContext.Rapps.AddAsync(model);
|
||||
await dbContext.SaveChangesAsync();
|
||||
await transaction.CommitAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
mex = ex.Message;
|
||||
}
|
||||
|
||||
return StatusCode(StatusCodes.Status200OK, mex);
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region MANUTENZIONI
|
||||
|
||||
199
ApiPolo/Data/TenantDbContext.cs
Normal file
199
ApiPolo/Data/TenantDbContext.cs
Normal file
@ -0,0 +1,199 @@
|
||||
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;
|
||||
|
||||
#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
|
||||
_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("DISCOMICOMIMP");
|
||||
mb.Entity<Compo_Impia_Table>().HasKey(table => new
|
||||
{
|
||||
table.cocodimp,
|
||||
table.cprownum,
|
||||
table.cocodazi
|
||||
});
|
||||
}, "DISCOMICOMIMP");
|
||||
|
||||
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("DISCOMAG_NEW");
|
||||
mb.Entity<Mag_New>().HasKey(table => new
|
||||
{
|
||||
table.seriale_rapportino,
|
||||
table.riga
|
||||
});
|
||||
}, "DISCOMAG_NEW");
|
||||
|
||||
TryConfigureEntity(mb => mb.Entity<Magazzini>().ToTable("DISCOMAGAZZIN"), "DISCOMAGAZZIN");
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,9 +1,9 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ApiPolo.Data;
|
||||
|
||||
namespace ApiPolo.Interfaces
|
||||
{
|
||||
public interface ITenantDbContextFactory
|
||||
{
|
||||
ITenantDbContext GetDbContext(string tenant);
|
||||
TenantDbContext GetDbContext(string tenant);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,31 +1,29 @@
|
||||
using ApiPolo.Data;
|
||||
using ApiPolo.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using static ApiPolo.Controllers.PoloController;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
/// <summary></summary>
|
||||
public class TenantDbContextFactory : ITenantDbContextFactory
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
/// <summary></summary>
|
||||
public TenantDbContextFactory(IServiceProvider serviceProvider)
|
||||
public TenantDbContextFactory(IServiceProvider serviceProvider, IConfiguration configuration)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public ITenantDbContext GetDbContext(string tenant)
|
||||
public TenantDbContext GetDbContext(string tenant)
|
||||
{
|
||||
// Create a scope to resolve DbContext
|
||||
var scope = _serviceProvider.CreateScope();
|
||||
var optionsBuilder = new DbContextOptionsBuilder<TenantDbContext>();
|
||||
|
||||
return tenant switch
|
||||
{
|
||||
Clienti.Maras => scope.ServiceProvider.GetRequiredService<Maras_DbContext>(),
|
||||
string connectionString = _configuration.GetConnectionString(tenant)
|
||||
?? throw new KeyNotFoundException($"No connection string found for tenant: {tenant}");
|
||||
|
||||
// Add other tenants as needed
|
||||
// Clienti.AnotherTenant => scope.ServiceProvider.GetRequiredService<AnotherTenant_DbContext>(),
|
||||
_ => throw new KeyNotFoundException($"No DbContext found for tenant: {tenant}")
|
||||
};
|
||||
optionsBuilder.UseSqlServer(connectionString);
|
||||
|
||||
return new TenantDbContext(optionsBuilder.Options, _configuration, tenant);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user