151 lines
5.6 KiB
C#
151 lines
5.6 KiB
C#
using EgwCoreLib.Lux.Data.DbModel.Cost;
|
|
using EgwCoreLib.Lux.Data.DbModel.Utils;
|
|
using EgwCoreLib.Lux.Data.DbModel.Production;
|
|
using EgwCoreLib.Lux.Data.DbModel.Sales;
|
|
using EgwCoreLib.Lux.Data.DbModel.Task;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using EgwCoreLib.Lux.Data.DbModel.Stock;
|
|
using EgwCoreLib.Lux.Data.DbModel.Items;
|
|
|
|
namespace EgwCoreLib.Lux.Data
|
|
{
|
|
public partial class DataLayerContext: DbContext
|
|
{
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private IConfiguration _configuration;
|
|
|
|
public DataLayerContext()
|
|
{
|
|
}
|
|
|
|
public DataLayerContext(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
}
|
|
|
|
public DataLayerContext(DbContextOptions<DataLayerContext> options) : base(options)
|
|
{
|
|
try
|
|
{
|
|
// se non ci fosse... crea o migra!
|
|
Database.Migrate();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error(exc, "Exception during context initialization 02");
|
|
}
|
|
}
|
|
|
|
|
|
public virtual DbSet<CounterModel> DbSetCounters { get; set; }
|
|
public virtual DbSet<ItemGroupModel> DbSetItemGroup { get; set; }
|
|
public virtual DbSet<ItemModel> DbSetItem { get; set; }
|
|
public virtual DbSet<SellingItemModel> DbSetSellItem { get; set; }
|
|
public virtual DbSet<TagsModel> DbSetTags { get; set; }
|
|
public virtual DbSet<CustomerModel> DbSetCustomer { get; set; }
|
|
public virtual DbSet<DealerModel> DbSetDealer { get; set; }
|
|
public virtual DbSet<SupplierModel> DbSetSupplier { get; set; }
|
|
public virtual DbSet<OfferModel> DbSetOffer { get; set; }
|
|
public virtual DbSet<OfferRowModel> DbSetOfferRow { get; set; }
|
|
public virtual DbSet<OrderModel> DbSetOrder { get; set; }
|
|
public virtual DbSet<OrderRowModel> DbSetORDerRow { get; set; }
|
|
public virtual DbSet<ResourceModel> DbSetResource { get; set; }
|
|
public virtual DbSet<PhaseModel> DbSetPhase { get; set; }
|
|
public virtual DbSet<JobModel> DbSetJob { get; set; }
|
|
public virtual DbSet<JobStepModel> DbSetJobRow { get; set; }
|
|
public virtual DbSet<JobStepItemModel> DbSetJobRowItem { get; set; }
|
|
public virtual DbSet<ProductionBatchModel> DbSetProdBatch { get; set; }
|
|
public virtual DbSet<ProductionItemModel> DbSetProdItem { get; set; }
|
|
public virtual DbSet<ProductionItemStepModel> DbSetProdItemRow { get; set; }
|
|
public virtual DbSet<StockStatusModel> DbSetStockStatus { get; set; }
|
|
public virtual DbSet<MovTypeModel> DbSetMovType { get; set; }
|
|
public virtual DbSet<StockMovModel> DbSetStockMov { get; set; }
|
|
public virtual DbSet<GenClassModel> DbSetGenClass { get; set; }
|
|
public virtual DbSet<GenValueModel> DbSetGenVal { get; set; }
|
|
|
|
|
|
|
|
|
|
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
// default
|
|
string connString = DbConfig.CONNECTION_STRING;
|
|
if (string.IsNullOrEmpty(connString))
|
|
{
|
|
#if DEBUG
|
|
connString = "Server=mdb.ufficio;port=3306;database=Lux_000_dev;uid=lux_user;pwd=Egal_pwd!;sslmode=None;";
|
|
#else
|
|
connString = "Server=mdb.ufficio;port=3306;database=Lux_000;uid=lux_user;pwd=Egal_pwd!;sslmode=None;";
|
|
#endif
|
|
}
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
var serverVersion = ServerVersion.AutoDetect(connString);
|
|
optionsBuilder.UseMySql(connString, serverVersion);
|
|
// verificare setup componente
|
|
#if false
|
|
optionsBuilder
|
|
.UseMySql(connString, serverVersion)
|
|
.UseSnakeCaseNamingConvention(); // via EFCore.NamingConventions
|
|
#endif
|
|
}
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
|
|
{
|
|
relationship.DeleteBehavior = DeleteBehavior.Restrict;
|
|
}
|
|
|
|
// fix chiavi multiple
|
|
modelBuilder.Entity<CounterModel>()
|
|
.HasKey(c => new { c.RefYear, c.CountName});
|
|
|
|
// fix valori timestamp
|
|
modelBuilder.Entity<StockMovModel>(entity =>
|
|
{
|
|
entity.Property(e => e.DtCreate)
|
|
.HasColumnType("timestamp")
|
|
.HasDefaultValueSql("CURRENT_TIMESTAMP")
|
|
.ValueGeneratedOnAdd();
|
|
|
|
entity.Property(e => e.DtMod)
|
|
.HasColumnType("timestamp")
|
|
.HasDefaultValueSql("CURRENT_TIMESTAMP")
|
|
.ValueGeneratedOnAddOrUpdate();
|
|
});
|
|
|
|
// seed finale dei dati di default
|
|
modelBuilder.Seed();
|
|
|
|
OnModelCreatingPartial(modelBuilder);
|
|
}
|
|
|
|
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");
|
|
}
|
|
}
|
|
}
|
|
}
|