167 lines
5.1 KiB
C#
167 lines
5.1 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using NKC.Data.DbModels;
|
|
using NLog;
|
|
|
|
namespace NKC.Data
|
|
{
|
|
public partial class NKCContext : DbContext
|
|
{
|
|
#region Private Fields
|
|
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private IConfiguration _configuration = null!;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Indispensabile x prima generazione migrations EFCore
|
|
/// </summary>
|
|
[Obsolete("This constructor should never be used directly, and is only needed to generate entityframework stuff. Connection string can be adapted as pleased.")]
|
|
public NKCContext()
|
|
{
|
|
}
|
|
|
|
public NKCContext(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
}
|
|
|
|
public NKCContext(DbContextOptions<NKCContext> 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<MaterialModel> DbSetMaterials { get; set; } = null!;
|
|
public virtual DbSet<RemnantsModel> DbSetRemnants { get; set; } = null!;
|
|
public virtual DbSet<MovMagModel> DbSetMovMag { get; set; } = null!;
|
|
public virtual DbSet<PrintJobQueue> DbSetPrintJobQueues { 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("NKC.DB");
|
|
if (!string.IsNullOrEmpty(connString))
|
|
{
|
|
optionsBuilder.UseSqlServer(connString);
|
|
}
|
|
else
|
|
{
|
|
optionsBuilder.UseSqlServer("Server=SQL2016DEV;Database=Sauder_NKC;Trusted_Connection=True;");
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<MaterialModel>(entity =>
|
|
{
|
|
|
|
entity.HasIndex(e => e.MatExtCode, "Idx_Materials_MatExtCode_Unique")
|
|
.IsUnique();
|
|
|
|
entity.Property(e => e.ApprovDate)
|
|
.HasColumnType("datetime")
|
|
.HasDefaultValueSql("(getdate())");
|
|
|
|
entity.Property(e => e.ApprovUser)
|
|
.HasMaxLength(50)
|
|
.HasDefaultValueSql("('')");
|
|
|
|
entity.Property(e => e.LMm)
|
|
.HasColumnType("decimal(18, 3)")
|
|
.HasColumnName("L_mm");
|
|
|
|
entity.Property(e => e.MatDesc)
|
|
.HasMaxLength(500)
|
|
.HasDefaultValueSql("('')")
|
|
.HasComment("Description");
|
|
|
|
entity.Property(e => e.TMm)
|
|
.HasColumnType("decimal(18, 3)")
|
|
.HasColumnName("T_mm");
|
|
|
|
entity.Property(e => e.WMm)
|
|
.HasColumnType("decimal(18, 3)")
|
|
.HasColumnName("W_mm");
|
|
});
|
|
|
|
modelBuilder.Entity<PrintJobQueue>(entity =>
|
|
{
|
|
entity.HasKey(e => e.IdxPrintJob);
|
|
|
|
entity.ToTable("PrintJobQueue");
|
|
|
|
entity.Property(e => e.DtEnd)
|
|
.HasColumnType("datetime")
|
|
.HasColumnName("dtEnd");
|
|
|
|
entity.Property(e => e.DtLastTry)
|
|
.HasColumnType("datetime")
|
|
.HasColumnName("dtLastTry");
|
|
|
|
entity.Property(e => e.DtStart)
|
|
.HasColumnType("datetime")
|
|
.HasColumnName("dtStart");
|
|
|
|
entity.Property(e => e.KeyParam).HasMaxLength(50);
|
|
|
|
entity.Property(e => e.PrtName)
|
|
.HasMaxLength(500)
|
|
.HasColumnName("prtName");
|
|
|
|
entity.Property(e => e.Stato).HasColumnName("stato");
|
|
|
|
entity.Property(e => e.TipoReport).HasMaxLength(250);
|
|
});
|
|
|
|
OnModelCreatingPartial(modelBuilder);
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Public Methods
|
|
|
|
public void DbForceMigrate()
|
|
{
|
|
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");
|
|
}
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |