150 lines
4.5 KiB
C#
150 lines
4.5 KiB
C#
using System;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using GWMS.Data.DatabaseModels;
|
|
using NLog;
|
|
using System.Linq;
|
|
|
|
namespace GWMS.Data
|
|
{
|
|
public partial class GWMSContext : DbContext
|
|
{
|
|
#region Private Fields
|
|
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private IConfiguration _configuration;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public GWMSContext()
|
|
{
|
|
}
|
|
|
|
public GWMSContext(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
}
|
|
|
|
public GWMSContext(DbContextOptions<GWMSContext> 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<RebootLogModel> DbRebootLog { get; set; }
|
|
|
|
public virtual DbSet<AlarmLogModel> DbSetAlarmLog { get; set; }
|
|
|
|
public virtual DbSet<ConfigModel> DbSetConfig { get; set; }
|
|
|
|
public virtual DbSet<ItemModel> DbSetItems { get; set; }
|
|
|
|
public virtual DbSet<AnKeyValModel> DbSetKeyVal { get; set; }
|
|
|
|
public virtual DbSet<ListValModel> DbSetListVal { get; set; }
|
|
|
|
public virtual DbSet<OrderModel> DbSetOrders { get; set; }
|
|
|
|
public virtual DbSet<ParamSendModel> DbSetParamSend { get; set; }
|
|
|
|
public virtual DbSet<ParamSetModel> DbSetParamSet { get; set; }
|
|
|
|
public virtual DbSet<PlantDetailModel> DbSetPlant { get; set; }
|
|
|
|
public virtual DbSet<PlantLogModel> DbSetPlantLog { get; set; }
|
|
|
|
public virtual DbSet<PlantStatusModel> DbSetPlantStatus { get; set; }
|
|
|
|
public virtual DbSet<WeekPlanModel> DbSetPlantSupplWeekPlan { get; set; }
|
|
|
|
public virtual DbSet<SupplierModel> DbSetSupplier { get; set; }
|
|
|
|
public virtual DbSet<TransporterModel> DbSetTransporter { 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)
|
|
{
|
|
// default
|
|
string connString = DbConfig.CONNECTION_STRING;
|
|
//if (string.IsNullOrEmpty(connString))
|
|
//{
|
|
// connString = "Server=localhost;port=3306;database=GWMS_PZZFRR;user=user_PZZFRR;pwd=pwd_M3T@n0;sslmode=None;";
|
|
//}
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
var serverVersion = ServerVersion.AutoDetect(connString);
|
|
optionsBuilder.UseMySql(connString, serverVersion);
|
|
}
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
|
|
{
|
|
relationship.DeleteBehavior = DeleteBehavior.Restrict;
|
|
}
|
|
|
|
modelBuilder.Entity<ConfigModel>(entity =>
|
|
{
|
|
entity.Property(e => e.ValStd)
|
|
.HasComment("Valore di default/riferimento per la variabile");
|
|
});
|
|
|
|
modelBuilder.Entity<ListValModel>().HasKey(c => new { c.TabName, c.FieldName, c.Val });
|
|
|
|
modelBuilder.Entity<PlantStatusModel>().HasKey(c => new { c.PlantId, c.FluxType });
|
|
|
|
modelBuilder.Entity<ParamSendModel>().HasKey(c => new { c.PlantId, c.ParamUid });
|
|
|
|
modelBuilder.Entity<PlantLogModel>().HasIndex(c => new { c.PlantId }, "IX_PlantLog_PlantId");
|
|
modelBuilder.Entity<PlantLogModel>().HasIndex(c => new { c.PlantId, c.DtEvent }, "IX_PlantLog_PlantDtEv");
|
|
|
|
modelBuilder.Seed();
|
|
|
|
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
|
|
}
|
|
} |