using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using MP.AppAuth.Models; using NLog; using System; namespace MP.AppAuth { public partial class AppAuthContext : DbContext { #region Private Fields private static NLog.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() { } public AppAuthContext(IConfiguration configuration) { _configuration = configuration; try { // se non ci fosse... crea o migra! Database.Migrate(); } catch (Exception exc) { Log.Error(exc, "Exception during context initialization 01"); } } public AppAuthContext(DbContextOptions options) : base(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 DbSetAnagraficaGruppi { get; set; } public virtual DbSet DbSetAnagOpr { get; set; } public virtual DbSet DbSetGruppi2Oper { get; set; } public virtual DbSet DbSetUpdMan { get; set; } public virtual DbSet DbSetVocabolario { get; set; } #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(entity => { entity.HasKey(e => new { e.Lingua, e.Lemma }); entity.ToTable("Vocabolario"); entity.Property(e => e.Lingua).HasMaxLength(3); entity.Property(e => e.Lemma).HasMaxLength(50); entity.Property(e => e.Traduzione) .IsRequired() .HasMaxLength(500); }); modelBuilder.Entity(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(entity => { entity.HasKey(e => new { e.MatrOpr, e.CodGruppo }); entity.ToTable("Gruppi2Operatori"); entity.Property(e => e.CodGruppo).HasMaxLength(50); }); // modelBuilder.Seed(); OnModelCreatingPartial(modelBuilder); } #endregion Protected Methods } }