156 lines
4.6 KiB
C#
156 lines
4.6 KiB
C#
using System;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
using Microsoft.Extensions.Configuration;
|
|
using GWMS.Data.DatabaseModels;
|
|
using NLog;
|
|
using System.Linq;
|
|
using GWMS.Data.Controllers;
|
|
|
|
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<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<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; }
|
|
|
|
public virtual DbSet<UserModel> DbSetUser { 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 = "Server=localhost;port=3306;database=GWMS;user=GWMS;pwd=GWMS_secret_pwd;sslmode=None;";
|
|
|
|
// tento setup da config
|
|
try
|
|
{
|
|
//string server = _configuration["DbConfig:Server"];
|
|
//string nKey = _configuration["DbConfig:nKey"];
|
|
//string sKey = _configuration["DbConfig:sKey"];
|
|
|
|
//DbConfig.InitDb(server, nKey, sKey);
|
|
//DbConfig.CheckUser(nKey, sKey);
|
|
|
|
// uso conn string calcolata
|
|
connString = DbConfig.CONNECTION_STRING;
|
|
}
|
|
catch
|
|
{ }
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
//connString = "Server=localhost;port=3306;database=GWMS;user=GWMS;pwd=GWMS_secret_pwd;sslmode=None;";
|
|
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.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
|
|
}
|
|
} |