Files
2026-05-08 11:56:12 +02:00

229 lines
7.8 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using MP.AppAuth.Models;
using NLog;
using System;
namespace MP.AppAuth
{
public partial class AppAuthContext : DbContext
{
#region Private Fields
private static Logger Log = LogManager.GetCurrentClassLogger();
private IConfiguration _configuration;
#endregion Private Fields
#region Public Constructors
[Obsolete("This constructor should never be used directly, and is only needed to generate entityframework stuff. Connection string can be adapted as pleased.")]
public AppAuthContext()
{
}
private DbContextOptions<AppAuthContext> _options;
public AppAuthContext(IConfiguration configuration)
{
_configuration = configuration;
string connStr = _configuration.GetConnectionString("MP.Land.Auth");
if (string.IsNullOrEmpty(connStr))
{
connStr = _configuration.GetConnectionString("MP.Land");
}
if (string.IsNullOrEmpty(connStr))
{
connStr = _configuration.GetConnectionString("MP.Data");
}
_options = new DbContextOptionsBuilder<AppAuthContext>()
.UseSqlServer(connStr)
.Options;
try
{
// se non ci fosse... crea o migra!
Database.Migrate();
}
catch (Exception exc)
{
Log.Error(exc, "Exception during context initialization 01");
}
}
public AppAuthContext(DbContextOptions<AppAuthContext> options) : base(options)
{
_options = options;
try
{
// se non ci fosse... crea o migra!
Database.Migrate();
}
catch (Exception exc)
{
Log.Error(exc, "Exception during context initialization 02");
}
}
#endregion Public Constructors
#region Public Properties
public virtual DbSet<AnagraficaGruppi> DbSetAnagraficaGruppi { get; set; }
public virtual DbSet<AnagraficaOperatori> DbSetAnagOpr { get; set; }
public virtual DbSet<Gruppi2Operatori> DbSetGruppi2Oper { get; set; }
public virtual DbSet<UpdMan> DbSetUpdMan { get; set; }
public virtual DbSet<VocabolarioModel> DbSetVocabolario { get; set; }
public virtual DbSet<PermessiModel> DbSetPermessi { get; set; } = null!;
public virtual DbSet<Permessi2FunzioneModel> DbSetPermessi2Funzione { get; set; } = null!;
#endregion Public Properties
#region Private Methods
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
#endregion Private Methods
#region Protected Methods
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
string connString = _configuration.GetConnectionString("MP.Land");
if (!string.IsNullOrEmpty(connString))
{
optionsBuilder.UseSqlServer(connString);
}
else
{
optionsBuilder.UseSqlServer("Server=SQL2016DEV;Database=MoonPro;Trusted_Connection=True;");
}
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("Relational:Collation", "SQL_Latin1_General_CP1_CI_AS");
modelBuilder.Entity<VocabolarioModel>(entity =>
{
entity.HasKey(e => new { e.Lingua, e.Lemma });
entity.Property(e => e.Lingua).HasMaxLength(3);
entity.Property(e => e.Lemma).HasMaxLength(50);
entity.Property(e => e.Traduzione)
.IsRequired()
.HasMaxLength(500);
});
modelBuilder.Entity<AnagraficaGruppi>(entity =>
{
entity.HasKey(e => e.CodGruppo);
entity.ToTable("AnagraficaGruppi");
entity.Property(e => e.CodGruppo).HasMaxLength(50);
entity.Property(e => e.DescrGruppo)
.IsRequired()
.HasMaxLength(250)
.HasDefaultValueSql("('')");
entity.Property(e => e.SelEnabled).HasComment("Indica se sia selezionabile a livello di tendina x inserimento BCode");
entity.Property(e => e.TipoGruppo)
.IsRequired()
.HasMaxLength(50)
.HasDefaultValueSql("('REPARTO')")
.HasComment("tipo gruppo: reparto (es x gestione operatori assegnati), GRUPPO FASE (es macchien che fanno lo stesso tipo di lavoro), ...");
});
modelBuilder.Entity<Gruppi2Operatori>(entity =>
{
entity.HasKey(e => new { e.MatrOpr, e.CodGruppo });
entity.ToTable("Gruppi2Operatori");
entity.Property(e => e.CodGruppo).HasMaxLength(50);
});
modelBuilder.Entity<PermessiModel>(entity =>
{
entity.HasKey(e => e.CodPermesso);
entity.ToTable("Permessi");
entity.Property(e => e.CodPermesso)
.HasMaxLength(50)
.IsUnicode(false)
.HasColumnName("COD_PERMESSO")
.UseCollation("Latin1_General_CI_AS");
entity.Property(e => e.Descrizione)
.HasMaxLength(50)
.IsUnicode(false)
.HasColumnName("DESCRIZIONE")
.UseCollation("Latin1_General_CI_AS");
entity.Property(e => e.Gruppo).HasColumnName("GRUPPO");
entity.Property(e => e.Nome)
.HasMaxLength(50)
.IsUnicode(false)
.HasColumnName("NOME")
.UseCollation("Latin1_General_CI_AS");
entity.Property(e => e.Numero).HasColumnName("NUMERO");
entity.Property(e => e.Url)
.HasMaxLength(250)
.IsUnicode(false)
.HasColumnName("URL")
.UseCollation("Latin1_General_CI_AS");
});
modelBuilder.Entity<Permessi2FunzioneModel>(entity =>
{
entity.HasKey(e => new { e.CodPermesso, e.CodFunzione });
entity.ToTable("Permessi2Funzione");
entity.Property(e => e.CodPermesso)
.HasMaxLength(50)
.IsUnicode(false)
.HasColumnName("COD_PERMESSO")
.UseCollation("Latin1_General_CI_AS");
entity.Property(e => e.CodFunzione)
.HasMaxLength(31)
.HasColumnName("COD_FUNZIONE")
.UseCollation("Latin1_General_CI_AS");
entity.Property(e => e.Readwrite)
.HasMaxLength(1)
.IsUnicode(false)
.HasColumnName("READWRITE")
.IsFixedLength()
.UseCollation("Latin1_General_CI_AS");
entity.HasOne(d => d.PermessiNav)
.WithMany(p => p.Permessi2FunzioneNav)
.HasForeignKey(d => d.CodPermesso)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Permessi2Funzione_Permessi");
});
//
modelBuilder.Seed();
OnModelCreatingPartial(modelBuilder);
}
#endregion Protected Methods
}
}