110 lines
3.9 KiB
C#
110 lines
3.9 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
|
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
using SMGen.Data.DbModels;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SMGen.Data
|
|
{
|
|
public partial class SMGDataContext : DbContext
|
|
{
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private IConfiguration _configuration;
|
|
public SMGDataContext(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
|
|
try
|
|
{
|
|
Database.Migrate();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error(exc, "Exception during context initialization 011");
|
|
}
|
|
}
|
|
|
|
public SMGDataContext(DbContextOptions<SMGDataContext> options, IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
bool disableMigrate = _configuration.GetValue<bool>("SetupOpt:DisableSMGMigrate");
|
|
if (!disableMigrate)
|
|
{
|
|
try
|
|
{
|
|
// se non ci fosse... crea o migra!
|
|
Database.Migrate();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error(exc, "Exception during context initialization 02");
|
|
}
|
|
}
|
|
}
|
|
|
|
public virtual DbSet<TransizioneIngressiModelTemp> DbSetTranIngTemp { get; set; } = null!;
|
|
public virtual DbSet<AnagEventiModelTemp> DbSetAnagEventiTemp { get; set; } = null!;
|
|
public virtual DbSet<AnagEventiModel> DbSetAnagEventi { get; set; } = null!;
|
|
public virtual DbSet<AnagStatiModel> DbSetAnagStati { get; set; } = null!;
|
|
public virtual DbSet<FamIngressiViewModel> DbSetFamIngressi { get; set; } = null!;
|
|
public virtual DbSet<FamStatiViewModel> DbSetFamStati { get; set; } = null!;
|
|
|
|
public void DbForceMigrate()
|
|
{
|
|
// verifico SE devo eseguire la migration del DB IDENT...
|
|
bool disableMigrate = _configuration.GetValue<bool>("SetupOpt:DisableSMGMigrate");
|
|
if (!disableMigrate)
|
|
{
|
|
try
|
|
{
|
|
// se non ci fosse... crea o migra!
|
|
Database.Migrate();
|
|
Log.Info("DbForceMigrate: done!");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error(exc, "DbForceMigrate: Exception during context initialization 01");
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
string connString = _configuration.GetConnectionString("SMGen.DB");
|
|
if (!string.IsNullOrEmpty(connString))
|
|
{
|
|
optionsBuilder.UseSqlServer(connString);
|
|
}
|
|
else
|
|
{
|
|
optionsBuilder.UseSqlServer("Server=SQL2016DEV;Database=MoonPro;Trusted_Connection=True;");
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.UseCollation("Latin1_General_CI_AS");
|
|
modelBuilder.Entity<TransizioneIngressiModelTemp>().HasKey(c => new { c.IdxFamigliaIngresso, c.IdxMicroStato, c.ValoreIngresso });
|
|
|
|
modelBuilder.Entity<FamIngressiViewModel>().ToView("v_FamIngressi");
|
|
modelBuilder.Entity<FamIngressiViewModel>().HasNoKey();
|
|
|
|
|
|
modelBuilder.Entity<FamStatiViewModel>().ToView("v_FamStati");
|
|
modelBuilder.Entity<FamStatiViewModel>().HasNoKey();
|
|
|
|
OnModelCreatingPartial(modelBuilder);
|
|
}
|
|
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
|
|
}
|
|
}
|