diff --git a/MP.Data/Controllers/MpInveController.cs b/MP.Data/Controllers/MpInveController.cs index 7e3ecb0b..aadbc604 100644 --- a/MP.Data/Controllers/MpInveController.cs +++ b/MP.Data/Controllers/MpInveController.cs @@ -17,6 +17,10 @@ namespace MP.Data.Controllers public MpInveController(IConfiguration configuration) { _configuration = configuration; + string connStr = _configuration.GetConnectionString("MP.Data"); + options = new DbContextOptionsBuilder() + .UseSqlServer(connStr) + .Options; Log.Info("Avviata classe MpInveController"); } @@ -84,6 +88,7 @@ namespace MP.Data.Controllers #endregion gestione articoli + private DbContextOptions options; #region gestione config /// @@ -93,7 +98,7 @@ namespace MP.Data.Controllers public List ConfigGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetConfig @@ -350,7 +355,7 @@ namespace MP.Data.Controllers public List ElencoOperatori() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbOperatori @@ -372,7 +377,7 @@ namespace MP.Data.Controllers { List dbResult = new List(); bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbOperatori @@ -644,7 +649,7 @@ namespace MP.Data.Controllers InventorySessionModel dbResult = new InventorySessionModel(); using (var dbCtx = new MoonPro_InveContext(_configuration)) { - + dbResult = dbCtx .DbInveSess .Where(x => x.InveSessID == sessID) diff --git a/MP.Data/Controllers/MpIocController.cs b/MP.Data/Controllers/MpIocController.cs index bbfd3984..cee027b3 100644 --- a/MP.Data/Controllers/MpIocController.cs +++ b/MP.Data/Controllers/MpIocController.cs @@ -13,13 +13,17 @@ using static MP.Core.Objects.Enums; namespace MP.Data.Controllers { - public class MpIocController /*: IDisposable*/ + public class MpIocController { #region Public Constructors public MpIocController(IConfiguration configuration) { _configuration = configuration; + string connStr = _configuration.GetConnectionString("MP.Data"); + options = new DbContextOptionsBuilder() + .UseSqlServer(connStr) + .Options; Log.Info("Avviata classe MpIocController"); } @@ -39,7 +43,7 @@ namespace MP.Data.Controllers /// public async Task AlarmLogInsertAsync(DateTime dtRif, string machineId, string memAddress, int memIndex, int statusVal, string valDecoded) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var DtRif = new SqlParameter("@DtRif", dtRif); var MachineId = new SqlParameter("@MachineId", machineId); @@ -59,7 +63,7 @@ namespace MP.Data.Controllers /// public async Task> AnagStatiGetAllAsync() { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var dbResult = await dbCtx .DbSetAnagStati @@ -77,7 +81,7 @@ namespace MP.Data.Controllers /// public async Task> ArticoliGetLastByMaccAsync(string idxMacc) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacc); var dbResult = await dbCtx @@ -103,7 +107,7 @@ namespace MP.Data.Controllers /// public async Task AutoStartOdlAsync(int idxOdl, int MatrOpr, string idxMacchina, decimal tCRich, int pzPallet, string note, bool startNewOdl, int qtyRich, string keyRich) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxOdl = new SqlParameter("@idxOdl ", idxOdl); var MatrApp = new SqlParameter("@MatrApp ", MatrOpr); @@ -134,7 +138,7 @@ namespace MP.Data.Controllers /// public async Task CheckCambiaStatoBatchAsync(tipoInputEvento tipoInput, string IdxMacchina, DateTime InizioStato, int IdxTipo, string CodArt, string Value, int MatrOpr, string pallet) { - await using var dbCtx = new MoonProContext(_configuration); + await using var dbCtx = new MoonProContext(options); await using var tx = await dbCtx.Database.BeginTransactionAsync(); try @@ -165,7 +169,7 @@ namespace MP.Data.Controllers _ => string.Empty }; - // ✅ prima chiamata SYNC + // ✅ prima chiamata ASYNC rigaTrans = (await dbCtx.DbSetSMES .FromSqlRaw(sql, pIdxMacchina, pIdxTipo) .AsNoTracking() @@ -220,7 +224,7 @@ namespace MP.Data.Controllers /// public async Task ConfermaProdMacchinaAsync(string idxMacchina, int modoConfProd, int numPzConfermati, int numPzScarto, DateTime DataOraApp, int MatrOpr) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var DataOra = new SqlParameter("@DataOra ", DateTime.Now); @@ -263,7 +267,7 @@ namespace MP.Data.Controllers /// public async Task ConfermaProdMacchinaFullAsync(string idxMacchina, int modoConfProd, int numPzConfermati, int numPzLasciati, int numPzScarto, DateTime DataOraApp, int MatrOpr) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var DataOra = new SqlParameter("@DataOra ", DateTime.Now); @@ -322,7 +326,7 @@ namespace MP.Data.Controllers /// public async Task> ConfigGetAllAsync() { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var dbResult = await dbCtx .DbSetConfig @@ -339,7 +343,7 @@ namespace MP.Data.Controllers /// public async Task ConfigUpdateAsync(ConfigModel updRec) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); bool fatto = false; var dbResult = dbCtx @@ -361,7 +365,7 @@ namespace MP.Data.Controllers /// public async Task> DatiMacchineGetAllAsync() { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var dbResult = await dbCtx .DbSetDatiMacchine @@ -384,7 +388,7 @@ namespace MP.Data.Controllers /// public async Task DDB_InsStatoBatchAsync(string idxMacchina, DateTime inizioStato, int idxStato, string codArt, string value, int matrOpr, string pallet) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var InizioStato = new SqlParameter("@InizioStato", inizioStato); @@ -407,7 +411,7 @@ namespace MP.Data.Controllers /// public async Task> DecNumArtGetFiltAsync(string codArt = "") { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var query = dbCtx.DbSetDecNumArt .AsNoTracking() @@ -421,13 +425,6 @@ namespace MP.Data.Controllers return dbResult; } -#if false - public void Dispose() - { - _configuration = null; - } -#endif - /// /// Stored x recuperare ultimi dossier macchina /// @@ -455,7 +452,7 @@ namespace MP.Data.Controllers /// public async Task EvListInsertAsync(EventListModel newRec) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); dbCtx.DbSetEvList.Add(newRec); return await dbCtx.SaveChangesAsync() > 0; @@ -470,7 +467,7 @@ namespace MP.Data.Controllers public async Task EvListMicroStatoInsertAsync(MicroStatoMacchinaModel newRecMsm, EventListModel newRecEv) { // eseguo in transazione... - await using var dbCtx = new MoonProContext(_configuration); + await using var dbCtx = new MoonProContext(options); await using var tx = await dbCtx.Database.BeginTransactionAsync(); try @@ -608,7 +605,7 @@ namespace MP.Data.Controllers public async Task KeepAliveUpsertAsync(string IdxMacc, DateTime OraServer, DateTime OraMacc) { bool fatto = false; - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var currRec = await dbCtx .DbSetKeepAlive @@ -641,7 +638,7 @@ namespace MP.Data.Controllers public async Task> ListLinkFiltAsync(string tipoLink) { List dbResult = new List(); - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); dbResult = await dbCtx .DbSetLinkMenu @@ -661,7 +658,7 @@ namespace MP.Data.Controllers public async Task> ListValuesFiltAsync(string tabName, string fieldName) { List dbResult = new List(); - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var query = dbCtx .DbSetListValues @@ -685,7 +682,7 @@ namespace MP.Data.Controllers public async Task> Macchine2SlaveAsync() { List dbResult = new List(); - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); dbResult = await dbCtx .DbSetM2S @@ -703,7 +700,7 @@ namespace MP.Data.Controllers public async Task> MacchineGetAllAsync() { List dbResult = new List(); - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); dbResult = await dbCtx .DbSetMacchine @@ -715,7 +712,7 @@ namespace MP.Data.Controllers public async Task MacchineGetByIdxAsync(string IdxMacchina) { MacchineModel dbResult = null; - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); dbResult = await dbCtx .DbSetMacchine @@ -732,7 +729,7 @@ namespace MP.Data.Controllers public async Task> MacchineGetFiltAsync(string codGruppo) { List dbResult = new List(); - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); if (codGruppo == "*") { @@ -766,7 +763,7 @@ namespace MP.Data.Controllers /// public async Task MacchineUpsertAsync(MacchineModel entity) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); // Recuperiamo l'entità tracciata dal context var trackedEntity = await dbCtx @@ -794,7 +791,7 @@ namespace MP.Data.Controllers public async Task> MicroStatoMacchinaGetByIdxMaccAsync(string IdxMacc) { List dbResult = new List(); - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); dbResult = await dbCtx .DbSetMicroStatoMacc @@ -812,7 +809,7 @@ namespace MP.Data.Controllers public async Task MicroStatoMacchinaUpsertAsync(MicroStatoMacchinaModel newRec) { bool fatto = false; - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var actRec = await dbCtx .DbSetMicroStatoMacc @@ -845,7 +842,7 @@ namespace MP.Data.Controllers public async Task> MseGetAllAsync(int maxAge = 2000) { List dbResult = new List(); - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var maxAgeSec = new SqlParameter("@maxAgeSec", maxAge); @@ -865,7 +862,7 @@ namespace MP.Data.Controllers /// public async Task OdlAutoDayGenAsync(string idxMacchina, DateTime dataInizio, DateTime dataFine, string codArticolo) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var DataInizio = new SqlParameter("@DataInizio", dataInizio); @@ -887,7 +884,7 @@ namespace MP.Data.Controllers /// public async Task OdlAutoDayGenFullAsync(string idxMacchina, DateTime dataInizio, DateTime dataFine, string codArticolo, int? pzPODL, int? pzPallet, string? keyRichiesta, int? tcAssegnato, string? codGruppo, bool flgCreaPODL, bool flgCheckTC) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var DataInizio = new SqlParameter("@DataInizio", dataInizio); @@ -916,7 +913,7 @@ namespace MP.Data.Controllers public async Task OdlCurrByMaccAsync(string idxMacchina) { ODLExpModel answ = new(); - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var pIdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); // attenzione: se la stored resituisce una tabella, il primo elemento va recuperato in RAM!!! @@ -939,7 +936,7 @@ namespace MP.Data.Controllers /// public async Task OdlFixMachineSlave(string idxMacchina, int numDayPrev, int doInsert) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var idxMaccParam = new SqlParameter("@IdxMacchina", idxMacchina ?? ""); var numDayPrevParam = new SqlParameter("@NumDayPrev", numDayPrev); @@ -961,7 +958,7 @@ namespace MP.Data.Controllers /// public async Task OdlFixMachineSlaveAsync(string idxMacchina, int numDayPrev, int doInsert) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxMacc = new SqlParameter("@IdxMacchina", idxMacchina); var NumDayPrev = new SqlParameter("@NumDayPrev", numDayPrev); @@ -980,7 +977,7 @@ namespace MP.Data.Controllers public async Task OdlLastByMaccAsync(string idxMacchina) { ODLExpModel answ = new(); - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var pIdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); // attenzione: se la stored resituisce una tabella, il primo elemento va recuperato in RAM!!! @@ -1004,7 +1001,7 @@ namespace MP.Data.Controllers public async Task> OdlListByMaccPeriodoAsync(string idxMacchina, DateTime dtStart, DateTime dtEnd) { List dbResult = new List(); - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var DataFrom = new SqlParameter("@dataFrom", dtStart); @@ -1026,7 +1023,7 @@ namespace MP.Data.Controllers public async Task PezziProdMacchinaAsync(string idxMacchina) { PzProdModel dbResult = new PzProdModel(); - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var pIdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); dbResult = (await dbCtx @@ -1049,7 +1046,7 @@ namespace MP.Data.Controllers public async Task> POdlGetByMaccArtAsync(string idxMacchina, string codArticolo, string codGruppo, bool onlyFree) { List dbResult = new List(); - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var pIdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var pCodArticolo = new SqlParameter("@CodArticolo", codArticolo); @@ -1072,7 +1069,7 @@ namespace MP.Data.Controllers /// public async Task RecalcMseAsync(string idxMacchina, int maxAgeSec) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var rigaProd = await StatoProdMacchinaAsync(idxMacchina, DateTime.Now); var MaxAgeSec = new SqlParameter("@maxAgeSec ", maxAgeSec); @@ -1096,7 +1093,7 @@ namespace MP.Data.Controllers /// public async Task RegControlliInsertAsync(string idxMacchina, int matrOpr, bool esitoOk, string note, DateTime dataOra) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxMacc = new SqlParameter("@IdxMacchina", idxMacchina); var MatrOpr = new SqlParameter("@MatrOpr", matrOpr); @@ -1117,7 +1114,7 @@ namespace MP.Data.Controllers /// public async Task RegDichiarInsertAsync(RegistroDichiarazioniModel newRec) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var TagCode = new SqlParameter("@TagCode", newRec.TagCode); var IdxMacchina = new SqlParameter("@IdxMacchina", newRec.IdxMacchina); @@ -1139,7 +1136,7 @@ namespace MP.Data.Controllers /// public async Task RegDichiarUpdateAsync(RegistroDichiarazioniModel newRec) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var Original_IdxDich = new SqlParameter("@Original_IdxDich", newRec.IdxDich); var DtRec = new SqlParameter("@DtRec", newRec.DtRec); @@ -1160,7 +1157,7 @@ namespace MP.Data.Controllers /// public async Task RegScartiInsertAsync(RegistroScartiModel newRec) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxMacchina = new SqlParameter("@idxMacchina", newRec.IdxMacchina); var DataOra = new SqlParameter("@DataOra", newRec.DataOra); @@ -1187,7 +1184,7 @@ namespace MP.Data.Controllers public async Task RemRebootLogAddAndCleanAsync(RemoteRebootLogModel newRec, bool doClean, int num2keep) { bool fatto = false; - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); // 1. Transazione minima: SOLO INSERT + COMMIT await using var tx = await dbCtx.Database.BeginTransactionAsync(); @@ -1230,7 +1227,7 @@ namespace MP.Data.Controllers /// public async Task RemRebootLogAddAsync(RemoteRebootLogModel newRec) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var dbResult = dbCtx .DbSetRemRebLog @@ -1245,7 +1242,7 @@ namespace MP.Data.Controllers /// public async Task> RemRebootLogGetAllAsync() { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var dbResult = await dbCtx .DbSetRemRebLog @@ -1261,7 +1258,7 @@ namespace MP.Data.Controllers /// public async Task> RemRebootLogGetLastAsync() { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var dbResult = await dbCtx .DbSetRemRebLog .FromSqlRaw("EXEC stp_RRL_getLast") @@ -1276,7 +1273,7 @@ namespace MP.Data.Controllers /// public async Task RemRebootLogKeepLastAsync(int num2keep) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var pNum2Keep = new SqlParameter("@num2keep", num2keep); // La SP gestisce già la logica di soglia (1.5x), ma la specifico x sicurezza @@ -1294,7 +1291,7 @@ namespace MP.Data.Controllers /// public async Task SignalLogInsertAsync(SignalLogModel newRec) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var currRec = dbCtx .DbSetSignalLog @@ -1310,7 +1307,7 @@ namespace MP.Data.Controllers /// public async Task> SMES_getHwTransitionsAsync(string idxMacchina, int idxTipo) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var IdxTipo = new SqlParameter("@IdxTipo", idxTipo); @@ -1330,7 +1327,7 @@ namespace MP.Data.Controllers /// public async Task> SMES_getUserForcedAsync(string idxMacchina, int idxTipo) { - await using var dbCtx = new MoonProContext(_configuration); + await using var dbCtx = new MoonProContext(options); var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var IdxTipo = new SqlParameter("@IdxTipo", idxTipo); @@ -1350,7 +1347,7 @@ namespace MP.Data.Controllers public List StateMachineIngressi(int idxFam) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxFamIn = new SqlParameter("@IdxFamigliaIngresso", idxFam); dbResult = dbCtx @@ -1369,7 +1366,7 @@ namespace MP.Data.Controllers /// public async Task> StateMachineIngressiAsync(int idxFam) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxFamIn = new SqlParameter("@IdxFamigliaIngresso", idxFam); var dbResult = await dbCtx @@ -1389,7 +1386,7 @@ namespace MP.Data.Controllers /// public async Task StatoProdMacchinaAsync(string idxMacchina, DateTime dtReq) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var DataOra = new SqlParameter("@DataOra ", dtReq); @@ -1409,7 +1406,7 @@ namespace MP.Data.Controllers /// public async Task> VMSFDGetAllAsync() { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var dbResult = await dbCtx .DbSetMSFD @@ -1427,7 +1424,7 @@ namespace MP.Data.Controllers /// public async Task VMSFDGetByMaccAsync(string idxMacc) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacc); var dbResult = (await dbCtx @@ -1447,7 +1444,7 @@ namespace MP.Data.Controllers /// public async Task> VMSFDGetMultiByMaccAsync(string idxMacc) { - using var dbCtx = new MoonProContext(_configuration); + using var dbCtx = new MoonProContext(options); var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacc); @@ -1465,9 +1462,16 @@ namespace MP.Data.Controllers #region Private Fields private static IConfiguration _configuration; - private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + private DbContextOptions options; #endregion Private Fields + +#if false + public void Dispose() + { + _configuration = null; + } +#endif } } \ No newline at end of file diff --git a/MP.Data/Controllers/MpLandController.cs b/MP.Data/Controllers/MpLandController.cs index e744b638..956c460c 100644 --- a/MP.Data/Controllers/MpLandController.cs +++ b/MP.Data/Controllers/MpLandController.cs @@ -1,16 +1,11 @@ -using Microsoft.Data.SqlClient; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; -using MP.Core.Objects; using MP.Data.DbModels; using NLog; using System; using System.Collections.Generic; using System.Data; using System.Linq; -using System.Threading.Tasks; -using ZXing; -using static MP.Core.Objects.Enums; namespace MP.Data.Controllers { @@ -24,6 +19,10 @@ namespace MP.Data.Controllers public MpLandController(IConfiguration configuration) { _configuration = configuration; + string connStr = _configuration.GetConnectionString("MP.Data"); + options = new DbContextOptionsBuilder() + .UseSqlServer(connStr) + .Options; Log.Info("Avviato MpLandController"); } @@ -67,7 +66,7 @@ namespace MP.Data.Controllers // leggo per DB principale if (!string.IsNullOrEmpty(_configuration.GetConnectionString("MP.All"))) { - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var singleRes = dbCtx .DbSetDbSize @@ -113,7 +112,7 @@ namespace MP.Data.Controllers } } } - catch(Exception exc) + catch (Exception exc) { Log.Error($"Eccezione in AllDbInfo:{Environment.NewLine}{exc}"); } @@ -127,7 +126,7 @@ namespace MP.Data.Controllers public List ConfigGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetConfig @@ -150,7 +149,7 @@ namespace MP.Data.Controllers public List ElencoOperatori() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbOperatori @@ -169,7 +168,7 @@ namespace MP.Data.Controllers public List MacchineGetAll() { List dbResult = new List(); - using (MoonProContext localDbCtx = new MoonProContext(_configuration)) + using (MoonProContext localDbCtx = new MoonProContext(options)) { dbResult = localDbCtx .DbSetMacchine @@ -185,7 +184,7 @@ namespace MP.Data.Controllers public List RemRebootLogGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetRemRebLog @@ -203,7 +202,7 @@ namespace MP.Data.Controllers public List RemRebootLogGetLast() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetRemRebLog @@ -221,7 +220,7 @@ namespace MP.Data.Controllers public List RemRebootLogGetLastNoMacc() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetRemRebLog @@ -262,8 +261,8 @@ namespace MP.Data.Controllers #region Private Fields private static IConfiguration _configuration; - private static Logger Log = LogManager.GetCurrentClassLogger(); + private DbContextOptions options; #endregion Private Fields } diff --git a/MP.Data/Controllers/MpMonController.cs b/MP.Data/Controllers/MpMonController.cs index 300bfe46..e45f9913 100644 --- a/MP.Data/Controllers/MpMonController.cs +++ b/MP.Data/Controllers/MpMonController.cs @@ -17,6 +17,11 @@ namespace MP.Data.Controllers public MpMonController(IConfiguration configuration) { _configuration = configuration; + + string connStr = _configuration.GetConnectionString("MP.Data"); + options = new DbContextOptionsBuilder() + .UseSqlServer(connStr) + .Options; Log.Info("Avviata classe MpMonController"); } @@ -33,7 +38,7 @@ namespace MP.Data.Controllers public List ArticoliGetSearch(int numRecord, string searchVal = "") { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetStatArticoli @@ -53,7 +58,7 @@ namespace MP.Data.Controllers public List ConfigGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetConfig @@ -75,7 +80,7 @@ namespace MP.Data.Controllers public List MacchineGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetMacchine @@ -96,7 +101,7 @@ namespace MP.Data.Controllers List dbResult = new List(); try { - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { if (codGruppo == "*") { @@ -140,7 +145,7 @@ namespace MP.Data.Controllers public async Task> MseGetAllAsync(int maxAge = 2000) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var maxAgeSec = new SqlParameter("@maxAgeSec", maxAge); @@ -161,7 +166,7 @@ namespace MP.Data.Controllers public bool RollBackEntity(object item) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -183,8 +188,8 @@ namespace MP.Data.Controllers #region Private Fields private static IConfiguration _configuration; - private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + private DbContextOptions options; #endregion Private Fields } diff --git a/MP.Data/Controllers/MpSpecController.cs b/MP.Data/Controllers/MpSpecController.cs index 99470205..efa33086 100644 --- a/MP.Data/Controllers/MpSpecController.cs +++ b/MP.Data/Controllers/MpSpecController.cs @@ -22,6 +22,10 @@ namespace MP.Data.Controllers public MpSpecController(IConfiguration configuration) { _configuration = configuration; + string connStr = _configuration.GetConnectionString("MP.Data"); + options = new DbContextOptionsBuilder() + .UseSqlServer(connStr) + .Options; Log.Info("Avviata classe MpSpecController"); } @@ -36,7 +40,7 @@ namespace MP.Data.Controllers public List AnagCounters() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetAnagCount @@ -56,7 +60,7 @@ namespace MP.Data.Controllers bool outTable = true; if (outTable) { - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var pCntType = new SqlParameter("@CntType", cntType); var pLastNum = new SqlParameter @@ -81,7 +85,7 @@ namespace MP.Data.Controllers else { // se si volessero impiegare parametri OUTPUT (qui ne mancherebbe 1 nella stored x CntCode...) - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var pCntType = new SqlParameter("@CntType", cntType); var pLastNum = new SqlParameter @@ -121,7 +125,7 @@ namespace MP.Data.Controllers public List AnagEventiGeneral(string TableName = "EvList", string FieldName = "Common") { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var pTableName = new SqlParameter("@TableName", TableName); var pFieldName = new SqlParameter("@FieldName", FieldName); @@ -142,7 +146,7 @@ namespace MP.Data.Controllers public List AnagEventiGetByMacc(string IdxMac) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacch = new SqlParameter("@idxMacchina", IdxMac); @@ -172,7 +176,7 @@ namespace MP.Data.Controllers public bool AnagGruppiDelete(AnagGruppiModel updRec) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var dbRec = dbCtx .DbSetAnagGruppi @@ -206,7 +210,7 @@ namespace MP.Data.Controllers public List AnagGruppiGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetAnagGruppi @@ -225,7 +229,7 @@ namespace MP.Data.Controllers public List AnagGruppiGetTipo(string tipoGruppo) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetAnagGruppi @@ -253,7 +257,7 @@ namespace MP.Data.Controllers public List AnagGruppiRepartoDTO() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { // in primis recupero i reparti... var listReparti = AnagGruppiGetTipo("REPARTO"); @@ -292,7 +296,7 @@ namespace MP.Data.Controllers public bool AnagGruppiUpsert(AnagGruppiModel updRec) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var dbRec = dbCtx .DbSetAnagGruppi @@ -323,7 +327,7 @@ namespace MP.Data.Controllers public List AnagKeyValGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetAKV @@ -379,7 +383,7 @@ namespace MP.Data.Controllers public async Task ArticoliDeleteRecord(AnagArticoliModel currRec) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -411,7 +415,7 @@ namespace MP.Data.Controllers public List ArticoliGetByTipo(string tipo, string azienda = "*") { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetArticoli @@ -432,7 +436,7 @@ namespace MP.Data.Controllers public List ArticoliGetSearch(int numRecord, string searchVal = "") { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetArticoli @@ -455,7 +459,7 @@ namespace MP.Data.Controllers public List ArticoliGetSearch(int numRecord, string azienda = "*", string searchVal = "") { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetArticoli @@ -475,7 +479,7 @@ namespace MP.Data.Controllers public List ArticoliGetUsed() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetArticoli @@ -494,7 +498,7 @@ namespace MP.Data.Controllers public async Task ArticoliUpdateRecord(AnagArticoliModel editRec) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -534,7 +538,7 @@ namespace MP.Data.Controllers public List ConfigGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetConfig @@ -553,7 +557,7 @@ namespace MP.Data.Controllers { bool fatto = false; ConfigModel dbResult = new ConfigModel(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetConfig @@ -576,7 +580,7 @@ namespace MP.Data.Controllers public List DatiMacchineGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetDatiMacchine @@ -776,7 +780,7 @@ namespace MP.Data.Controllers public List ElencoOperatori() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbOperatori @@ -796,7 +800,7 @@ namespace MP.Data.Controllers public async Task EvListInsert(EventListModel newRec) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1048,7 +1052,7 @@ namespace MP.Data.Controllers public bool Grp2MaccDelete(Gruppi2MaccModel rec2del) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var dbRec = dbCtx .DbSetGrp2Macc @@ -1072,7 +1076,7 @@ namespace MP.Data.Controllers public bool Grp2MaccInsert(Gruppi2MaccModel upsRec) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var dbRec = dbCtx .DbSetGrp2Macc @@ -1097,7 +1101,7 @@ namespace MP.Data.Controllers public bool Grp2OperDelete(Gruppi2OperModel rec2del) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var dbRec = dbCtx .DbSetGrp2Oper @@ -1121,7 +1125,7 @@ namespace MP.Data.Controllers public bool Grp2OperInsert(Gruppi2OperModel upsRec) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var dbRec = dbCtx .DbSetGrp2Oper @@ -1145,7 +1149,7 @@ namespace MP.Data.Controllers public bool IstKitDelete(IstanzeKitModel rec2del) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var actRec = dbCtx .DbSetInstKit @@ -1173,7 +1177,7 @@ namespace MP.Data.Controllers public List IstKitFilt(string keyKit, string keyExtOrd) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetInstKit @@ -1192,7 +1196,7 @@ namespace MP.Data.Controllers public bool IstKitInsertByWKS(string CodArtParent, string KeyFilt) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var pCodArtParent = new SqlParameter("@CodArtParent", CodArtParent); var pKeyFilt = new SqlParameter("@KeyFilt", KeyFilt); @@ -1212,7 +1216,7 @@ namespace MP.Data.Controllers public bool IstKitUpsert(IstanzeKitModel editRec) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var actRec = dbCtx .DbSetInstKit @@ -1274,7 +1278,7 @@ namespace MP.Data.Controllers public List ListLinkAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetLinkMenu @@ -1293,7 +1297,7 @@ namespace MP.Data.Controllers public List ListLinkFilt(string tipoLink) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetLinkMenu @@ -1319,7 +1323,7 @@ namespace MP.Data.Controllers public List ListODLFilt(bool inCorso, string codArt, string keyRichPart, string Reparto, string IdxMacchina, DateTime startDate, DateTime endDate) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var InCorso = new SqlParameter("@InCorso", inCorso); var CodArt = new SqlParameter("@CodArt", codArt); @@ -1347,7 +1351,7 @@ namespace MP.Data.Controllers public List ListPODL_ByCodArt(string CodArticolo, bool OnlyAvail) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var pCodArticolo = new SqlParameter("@CodArticolo", CodArticolo); var pOnlyAvail = new SqlParameter("@onlyAvail", OnlyAvail); @@ -1370,7 +1374,7 @@ namespace MP.Data.Controllers public List ListPODL_ByKitParent(int IdxPodlParent) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var pIdxPodlParent = new SqlParameter("@IdxPodlParent", IdxPodlParent); @@ -1394,7 +1398,7 @@ namespace MP.Data.Controllers public List ListPODL_KitFilt(bool lanciato, string keyRichPart, string idxMacchina, string codGruppo, DateTime startDate, DateTime endDate) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var Lanc = new SqlParameter("@Lanciato", lanciato); var KeyRich = new SqlParameter("@KeyRich", keyRichPart); @@ -1423,7 +1427,7 @@ namespace MP.Data.Controllers public List ListPODLFilt(bool lanciato, string keyRichPart, string idxMacchina, string codGruppo, DateTime startDate, DateTime endDate) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var Lanc = new SqlParameter("@Lanciato", lanciato); var KeyRich = new SqlParameter("@KeyRich", keyRichPart); @@ -1450,7 +1454,7 @@ namespace MP.Data.Controllers public List ListValuesFilt(string tabName, string fieldName) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetListValues @@ -1472,7 +1476,7 @@ namespace MP.Data.Controllers List dbResult = new List(); try { - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { if (MatrOpr == 0) { @@ -1522,7 +1526,7 @@ namespace MP.Data.Controllers List dbResult = new List(); try { - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { if (codGruppo == "*") { @@ -1577,27 +1581,6 @@ namespace MP.Data.Controllers return dbResult; } - /// - /// Elenco da tabella MappaStatoExplModel - /// - /// - public List MseGetAll(int maxAge = 2000) - { - List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) - { - var maxAgeSec = new SqlParameter("@maxAgeSec", maxAge); - - dbResult = dbCtx - .DbSetMSE - .FromSqlRaw("EXEC stp_MSE_getData @maxAgeSec", maxAgeSec) - .AsNoTracking() - .ToList(); - } - return dbResult; - } - - /// /// Aggiornamento record Microstato macchina /// @@ -1606,7 +1589,7 @@ namespace MP.Data.Controllers public async Task MicroStatoMacchinaUpsert(MicroStatoMacchinaModel newRec) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var actRec = await dbCtx .DbSetMicroStatoMacc @@ -1633,6 +1616,26 @@ namespace MP.Data.Controllers return fatto; } + /// + /// Elenco da tabella MappaStatoExplModel + /// + /// + public List MseGetAll(int maxAge = 2000) + { + List dbResult = new List(); + using (var dbCtx = new MoonProContext(options)) + { + var maxAgeSec = new SqlParameter("@maxAgeSec", maxAge); + + dbResult = dbCtx + .DbSetMSE + .FromSqlRaw("EXEC stp_MSE_getData @maxAgeSec", maxAgeSec) + .AsNoTracking() + .ToList(); + } + return dbResult; + } + /// /// Elenco ODL dato batch selezionato /// @@ -1667,7 +1670,7 @@ namespace MP.Data.Controllers public ODLExpModel OdlByKey(int IdxOdl) { ODLExpModel dbResult = new ODLExpModel(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1701,7 +1704,7 @@ namespace MP.Data.Controllers await Task.Delay(1); if (idxOdl > 0) { - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { DateTime adesso = DateTime.Now; // preparo i parametri @@ -1776,7 +1779,7 @@ namespace MP.Data.Controllers public async Task OdlGetByKey(int idxOdl) { ODLModel dbResult = new ODLModel(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = await dbCtx .DbSetODL @@ -1792,7 +1795,7 @@ namespace MP.Data.Controllers public List OdlGetCurrent() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetODL @@ -1809,7 +1812,7 @@ namespace MP.Data.Controllers public List OdlListAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1835,7 +1838,7 @@ namespace MP.Data.Controllers List dbResult = new List(); if (IdxOdl > 0) { - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxODL = new SqlParameter("@IdxODL", IdxOdl); @@ -1859,7 +1862,7 @@ namespace MP.Data.Controllers List dbResult = new List(); try { - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { if (codGruppo == "*") { @@ -1923,7 +1926,7 @@ namespace MP.Data.Controllers public PzProdModel PezziProdMacchina(string idxMacchina) { PzProdModel dbResult = new PzProdModel(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); dbResult = dbCtx @@ -1943,7 +1946,7 @@ namespace MP.Data.Controllers public async Task PODL_getByKey(int idxPODL) { PODLModel dbResult = new PODLModel(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1971,7 +1974,7 @@ namespace MP.Data.Controllers public PODLModel PODL_getByOdl(int idxODL) { PODLModel dbResult = new PODLModel(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -2021,7 +2024,7 @@ namespace MP.Data.Controllers InsertDate = editRec.InsertDate }; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -2066,7 +2069,7 @@ namespace MP.Data.Controllers public async Task PODL_updateRecipe(int idxPODL, string recipeName) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -2118,7 +2121,7 @@ namespace MP.Data.Controllers InsertDate = currRec.InsertDate }; bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -2147,7 +2150,7 @@ namespace MP.Data.Controllers public bool PodlIstKitDelete(int IdxPODL) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var pIdxPODL = new SqlParameter("@IdxPODL", IdxPODL); @@ -2167,7 +2170,7 @@ namespace MP.Data.Controllers public async Task PODLUpdateRecord(PODLModel editRec) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -2213,7 +2216,7 @@ namespace MP.Data.Controllers public bool RollBackEntity(object item) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -2238,7 +2241,7 @@ namespace MP.Data.Controllers public StatoMacchineModel StatoMacchina(string idxMacchina) { StatoMacchineModel dbResult = new StatoMacchineModel(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetStatoMacc @@ -2256,7 +2259,7 @@ namespace MP.Data.Controllers public bool TemplateKitDelete(TemplateKitModel rec2del) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var actRec = dbCtx .DbSetTempKit @@ -2284,7 +2287,7 @@ namespace MP.Data.Controllers public List TemplateKitFilt(string KitCode, string codChild) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetTempKit @@ -2303,7 +2306,7 @@ namespace MP.Data.Controllers public bool TemplateKitUpsert(TemplateKitModel editRec, string codAzienda) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { // verifico preliminarmente articolo... var recArt = dbCtx @@ -2363,7 +2366,7 @@ namespace MP.Data.Controllers List dbResult = new List(); if (!string.IsNullOrEmpty(KeyFilt)) { - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var pKeyFilt = new SqlParameter("@KeyFilt", KeyFilt); var pMaxRes = new SqlParameter("@maxResult", MaxResult); @@ -2385,7 +2388,7 @@ namespace MP.Data.Controllers public List VocabolarioGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetVocabolario @@ -2403,7 +2406,7 @@ namespace MP.Data.Controllers public bool WipKitDelete(WipSetupKitModel rec2del) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var actRec = dbCtx .DbSetWipKit @@ -2429,7 +2432,7 @@ namespace MP.Data.Controllers public bool WipKitDeleteGroup(string KeyFilt) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var actRec = dbCtx .DbSetWipKit @@ -2456,7 +2459,7 @@ namespace MP.Data.Controllers public bool WipKitDeleteOlder(DateTime dateLimit) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var actRec = dbCtx .DbSetWipKit @@ -2486,7 +2489,7 @@ namespace MP.Data.Controllers // solo se filtro valido... if (!string.IsNullOrEmpty(KeyFilt)) { - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetWipKit @@ -2505,7 +2508,7 @@ namespace MP.Data.Controllers public bool WipKitUpsert(WipSetupKitModel editRec) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var actRec = dbCtx .DbSetWipKit @@ -2538,8 +2541,8 @@ namespace MP.Data.Controllers #region Private Fields private static IConfiguration _configuration; - private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + private DbContextOptions options; #endregion Private Fields } diff --git a/MP.Data/Controllers/MpTabController.cs b/MP.Data/Controllers/MpTabController.cs index fe754d52..55981185 100644 --- a/MP.Data/Controllers/MpTabController.cs +++ b/MP.Data/Controllers/MpTabController.cs @@ -20,6 +20,10 @@ namespace MP.Data.Controllers public MpTabController(IConfiguration configuration) { _configuration = configuration; + string connStr = _configuration.GetConnectionString("MP.Data"); + options = new DbContextOptionsBuilder() + .UseSqlServer(connStr) + .Options; Log.Info("Avviato MpTabController"); } @@ -40,7 +44,7 @@ namespace MP.Data.Controllers public bool AlarmLogInsert(DateTime dtRif, string machineId, string memAddress, int memIndex, int statusVal, string valDecoded) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var DtRif = new SqlParameter("@DtRif", dtRif); var MachineId = new SqlParameter("@MachineId", machineId); @@ -68,7 +72,7 @@ namespace MP.Data.Controllers public List AlarmLogListFilt(string idxMacchina, DateTime dtFrom, DateTime dtTo, bool showMulti) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacc = new SqlParameter("@IdxMacchina", idxMacchina); var DtFrom = new SqlParameter("@dtFrom", dtFrom); @@ -94,7 +98,7 @@ namespace MP.Data.Controllers public bool AlarmLogSetAck(int alarmLogId, DateTime dtAck, string userAck) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var AlarmLogId = new SqlParameter("@MachineId", alarmLogId); var DtAck = new SqlParameter("@DtRif", dtAck); @@ -117,7 +121,7 @@ namespace MP.Data.Controllers public bool AlarmLogSetNotify(int alarmLogId, DateTime dtNotify) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var AlarmLogId = new SqlParameter("@MachineId", alarmLogId); var DtNotify = new SqlParameter("@DtNotify", dtNotify); @@ -137,7 +141,7 @@ namespace MP.Data.Controllers public List AnagEventiGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetAnagEventi @@ -154,7 +158,7 @@ namespace MP.Data.Controllers public List AnagEventiGetByMacc(string IdxMac) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacch = new SqlParameter("@idxMacchina", IdxMac); @@ -175,7 +179,7 @@ namespace MP.Data.Controllers public List AnagStatiGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetAnagStati @@ -192,7 +196,7 @@ namespace MP.Data.Controllers public List AnagTagsOrd() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetAnagTags @@ -214,7 +218,7 @@ namespace MP.Data.Controllers public List ArticoliGetByTipo(string tipo, string azienda = "*") { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetArticoli @@ -319,7 +323,7 @@ namespace MP.Data.Controllers public List CommentiGetLastByMacc(string idxMacchina, int numDays) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var NumDays = new SqlParameter("@NumDays", numDays); @@ -351,7 +355,7 @@ namespace MP.Data.Controllers try { var rigaProd = StatoProdMacchina(idxMacchina, DateTime.Now); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@idxMacchina", idxMacchina); var MatrApp = new SqlParameter("@MatrApp ", MatrOpr); @@ -398,7 +402,7 @@ namespace MP.Data.Controllers try { var rigaProd = StatoProdMacchina(idxMacchina, DateTime.Now); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@idxMacchina", idxMacchina); var MatrApp = new SqlParameter("@MatrApp ", MatrOpr); @@ -433,7 +437,7 @@ namespace MP.Data.Controllers public List ConfigGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetConfig @@ -457,7 +461,7 @@ namespace MP.Data.Controllers public bool DDB_DoRecalc(string idxMacchina, DateTime inizio, int idxStatoStart, int nStepEventi, int nRecCheck, bool checkOnly) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var Inizio = new SqlParameter("@inizio", inizio); @@ -485,7 +489,7 @@ namespace MP.Data.Controllers public DiarioDiBordoModel DDB_getNext(string idxMacchina, DateTime inizioStato) { DiarioDiBordoModel dbResult = new DiarioDiBordoModel(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var DataRif = new SqlParameter("@dataRif", inizioStato); @@ -514,7 +518,7 @@ namespace MP.Data.Controllers public bool DDB_InsStatoBatch(string idxMacchina, DateTime inizioStato, int idxStato, string codArt, string value, int matrOpr, string pallet) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var InizioStato = new SqlParameter("@InizioStato", inizioStato); @@ -549,7 +553,7 @@ namespace MP.Data.Controllers public List ElencoConfProdFilt(string idxMacchina, DateTime dataFrom, DateTime dataTo) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetElConfProd @@ -595,7 +599,7 @@ namespace MP.Data.Controllers public List ElencoOperatori() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbOperatori @@ -617,7 +621,7 @@ namespace MP.Data.Controllers public bool EvListDelete(string idxMacchina, DateTime dtEvento, int idxTipo) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var InizioStato = new SqlParameter("@InizioStato", dtEvento); @@ -651,7 +655,7 @@ namespace MP.Data.Controllers public async Task> EvListGetLastBySearch(string idxMacchina, DateTime dtLimit, int idxTipo, int maxRec) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = await dbCtx .DbSetEvList @@ -673,7 +677,7 @@ namespace MP.Data.Controllers public async Task EvListInsert(EventListModel newRec) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -701,7 +705,7 @@ namespace MP.Data.Controllers public List FermiNonQualificatiFilt(string idxMacchina, int gg, double durataMin) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var GG = new SqlParameter("@gg", gg); @@ -724,7 +728,7 @@ namespace MP.Data.Controllers public bool InsManDelete(InsManualiModel currRecord) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var actRec = dbCtx .DbSetInsManuali @@ -753,7 +757,7 @@ namespace MP.Data.Controllers public List InsManFilt(string IdxMacc, DateTime dtStart, DateTime dtEnd) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetInsManuali @@ -773,7 +777,7 @@ namespace MP.Data.Controllers public async Task InsManFreezeDay(string idxMacchina, DateTime dtCurr) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacc = new SqlParameter("@IdxMacchina", idxMacchina); var DataElab = new SqlParameter("@pDataOra", dtCurr); @@ -794,7 +798,7 @@ namespace MP.Data.Controllers public bool InsManUpsert(InsManualiModel currRecord) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var actRec = dbCtx .DbSetInsManuali @@ -835,7 +839,7 @@ namespace MP.Data.Controllers public bool InsManUpsert(List listRecord) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { foreach (var currRecord in listRecord) { @@ -880,7 +884,7 @@ namespace MP.Data.Controllers public List ListPODL_ByKitParent(int IdxPodlParent) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var pIdxPodlParent = new SqlParameter("@IdxPodlParent", IdxPodlParent); @@ -903,7 +907,7 @@ namespace MP.Data.Controllers public List ListPODLByMacc(string idxMacchina, bool onlyFree, bool onlyDirect) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacc = new SqlParameter("@IdxMacchina", idxMacchina); var OnlyFree = new SqlParameter("@onlyFree", onlyFree); @@ -927,7 +931,7 @@ namespace MP.Data.Controllers public List ListValuesFilt(string tabName, string fieldName) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetListValues @@ -948,7 +952,7 @@ namespace MP.Data.Controllers public bool MacchinaSetInsEnab(string idxMacchina, bool insEnabled) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@idxMacchina", idxMacchina); var InsEnabled = new SqlParameter("@insEnabled", insEnabled); @@ -969,7 +973,7 @@ namespace MP.Data.Controllers public List Macchine2Slave() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetM2S @@ -990,7 +994,7 @@ namespace MP.Data.Controllers List dbResult = new List(); try { - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { if (MatrOpr == 0) { @@ -1038,7 +1042,7 @@ namespace MP.Data.Controllers public MicroStatoMacchinaModel MicroStatoMacchina(string idxMacchina) { MicroStatoMacchinaModel dbResult = new MicroStatoMacchinaModel(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetMicroStatoMacc @@ -1058,7 +1062,7 @@ namespace MP.Data.Controllers public List MicroStatoMacchinaGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetMicroStatoMacc @@ -1076,7 +1080,7 @@ namespace MP.Data.Controllers public bool MicroStatoMacchinaUpsert(MicroStatoMacchinaModel newRec) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var actRec = dbCtx .DbSetMicroStatoMacc @@ -1112,7 +1116,7 @@ namespace MP.Data.Controllers public List MseGetSub(string idxMacc, string idxMacchSub) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1141,7 +1145,7 @@ namespace MP.Data.Controllers public List OdlByIdx(int idxOdl, bool onlyUnused) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1170,7 +1174,7 @@ namespace MP.Data.Controllers public bool OdlClearSetup(int idxODL, string idxMacchina) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1197,7 +1201,7 @@ namespace MP.Data.Controllers public List OdlCurrByMacc(string idxMacchina) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1226,7 +1230,7 @@ namespace MP.Data.Controllers public bool OdlDividiDaAltraTavola(int idxODL, int matrOpr, string idxMacchinaTo) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1256,7 +1260,7 @@ namespace MP.Data.Controllers public bool OdlFineProd(int idxODL, string idxMacchina, DateTime dtRif) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1286,7 +1290,7 @@ namespace MP.Data.Controllers public bool OdlFixMachineSlave(string idxMacchina, int numDayPrev, int doInsert) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1319,7 +1323,7 @@ namespace MP.Data.Controllers public bool OdlInizioSetup(int idxODL, int matrOpr, string idxMacchina, decimal tcRich, int pzPallet, string note) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1350,7 +1354,7 @@ namespace MP.Data.Controllers public List OdlLastByMacc(string idxMacchina) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1379,7 +1383,7 @@ namespace MP.Data.Controllers public List OdlListByMaccPeriodo(string idxMacchina, DateTime dtStart, DateTime dtEnd) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1408,7 +1412,7 @@ namespace MP.Data.Controllers public List OdlReopenOdlMacc(string idxMacchina) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1436,7 +1440,7 @@ namespace MP.Data.Controllers public bool OdlSetupPostumo(int idxODL, string idxMacchina) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1465,7 +1469,7 @@ namespace MP.Data.Controllers public bool OdlSetupPromPostuma(int idxPromOdl, int matrOpr, string idxMacchina) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1500,7 +1504,7 @@ namespace MP.Data.Controllers public bool OdlSplit(int idxODL, int matrOpr, string idxMacchina, decimal TCRich, int pzPallet, string note, bool startNewOdl, int qtyRich) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1537,7 +1541,7 @@ namespace MP.Data.Controllers public bool OdlUpdate(int idxODL, int matrOpr, decimal tCRichAttr, int pzPallet, string note) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1571,7 +1575,7 @@ namespace MP.Data.Controllers { AnagOperatoriModel dbResult = null; AnagOperatoriModel answ = new AnagOperatoriModel(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbOperatori @@ -1594,7 +1598,7 @@ namespace MP.Data.Controllers public List PezziProdMacchina(string idxMacchina) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); try @@ -1644,7 +1648,7 @@ namespace MP.Data.Controllers InsertDate = editRec.InsertDate }; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1688,7 +1692,7 @@ namespace MP.Data.Controllers public PODLExpModel PODLExp_getByKey(int idxPODL) { PODLExpModel dbResult = new PODLExpModel(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1719,7 +1723,7 @@ namespace MP.Data.Controllers public async Task PODLExp_getByKeyAsync(int idxPODL) { PODLExpModel dbResult = new PODLExpModel(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { try { @@ -1753,7 +1757,7 @@ namespace MP.Data.Controllers public List RegControlliFilt(string idxMacchina, int idxODL, DateTime dataFrom, DateTime dataTo, bool showMulti) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacc = new SqlParameter("@IdxMacchina", idxMacchina); var IdxODL = new SqlParameter("@IdxODL", idxODL); @@ -1782,7 +1786,7 @@ namespace MP.Data.Controllers public bool RegControlliInsert(string idxMacchina, int matrOpr, bool esitoOk, string note, DateTime dataOra) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacc = new SqlParameter("@IdxMacchina", idxMacchina); var MatrOpr = new SqlParameter("@MatrOpr", matrOpr); @@ -1804,7 +1808,7 @@ namespace MP.Data.Controllers public List RegControlliLast(string idxMacchina) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacc = new SqlParameter("@IdxMacchina", idxMacchina); @@ -1829,7 +1833,7 @@ namespace MP.Data.Controllers public List RegDichiarGetFilt(string idxMacchina, string tagCode, int matrOpr, int idxODL, DateTime dataFrom, DateTime dataTo) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var TagCode = new SqlParameter("@TagCode", tagCode); var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); @@ -1855,7 +1859,7 @@ namespace MP.Data.Controllers public async Task RegDichiarInsert(RegistroDichiarazioniModel newRec) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var TagCode = new SqlParameter("@TagCode", newRec.TagCode); var IdxMacchina = new SqlParameter("@IdxMacchina", newRec.IdxMacchina); @@ -1880,7 +1884,7 @@ namespace MP.Data.Controllers public async Task RegDichiarUpdate(RegistroDichiarazioniModel newRec) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var Original_IdxDich = new SqlParameter("@Original_IdxDich", newRec.IdxDich); var DtRec = new SqlParameter("@DtRec", newRec.DtRec); @@ -1909,7 +1913,7 @@ namespace MP.Data.Controllers public List RegScartiGetFilt(string idxMacchina, int idxODL, DateTime dataFrom, DateTime dataTo, bool showMulti) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacc = new SqlParameter("@IdxMacchina", idxMacchina); var IdxODL = new SqlParameter("@IdxODL", idxODL); @@ -1934,7 +1938,7 @@ namespace MP.Data.Controllers public bool RegScartiInsert(RegistroScartiModel newRec) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@idxMacchina", newRec.IdxMacchina); var DataOra = new SqlParameter("@DataOra", newRec.DataOra); @@ -1963,7 +1967,7 @@ namespace MP.Data.Controllers public bool RegScartiKitDelete(RegistroScartiModel parentRec) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacc = new SqlParameter("@IdxMacchina", parentRec.IdxMacchina); var DtRif = new SqlParameter("@DataOra", parentRec.DataOra); @@ -1986,7 +1990,7 @@ namespace MP.Data.Controllers public List RegScartiKitGetFilt(RegistroScartiModel parentRec) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacc = new SqlParameter("@IdxMacchina", parentRec.IdxMacchina); var DtRif = new SqlParameter("@DataRif", parentRec.DataOra); @@ -2010,7 +2014,7 @@ namespace MP.Data.Controllers { await Task.Delay(1); bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@idxMacchina", newRec.IdxMacchina); var DataOra = new SqlParameter("@DataOra", newRec.DataOra); @@ -2037,7 +2041,7 @@ namespace MP.Data.Controllers public bool RegScartiKitUpdateQty(RegistroScartiKitModel currRec) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacc = new SqlParameter("@IdxMacchina", currRec.IdxMacchina); var DtRif = new SqlParameter("@DataOra", currRec.DataOra); @@ -2066,7 +2070,7 @@ namespace MP.Data.Controllers try { var rigaProd = StatoProdMacchina(idxMacchina, DateTime.Now); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var MaxAgeSec = new SqlParameter("@maxAgeSec ", maxAgeSec); var IdxMacchina = new SqlParameter("@idxMacchina", idxMacchina); @@ -2098,7 +2102,7 @@ namespace MP.Data.Controllers public async Task RipristinaStatoPrec(string idxMacchina, DateTime dtCurr, string valore, int idxStato = 0, int matrOpr = 0) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var pIdxMacc = new SqlParameter("@IdxMacchina", idxMacchina); var pDataOra = new SqlParameter("@DataOra", dtCurr); @@ -2139,7 +2143,7 @@ namespace MP.Data.Controllers public List SMES_GetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetSMES @@ -2156,7 +2160,7 @@ namespace MP.Data.Controllers public List SMES_GetByFam(int idxFam) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetSMES @@ -2175,7 +2179,7 @@ namespace MP.Data.Controllers public List SMES_getHwTransitions(string idxMacchina, int idxTipo) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var IdxTipo = new SqlParameter("@IdxTipo", idxTipo); @@ -2197,7 +2201,7 @@ namespace MP.Data.Controllers public List SMES_getUserForced(string idxMacchina, int idxTipo) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var IdxTipo = new SqlParameter("@IdxTipo", idxTipo); @@ -2218,7 +2222,7 @@ namespace MP.Data.Controllers public List ST_AnagGruppiList() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetStAnagGruppi @@ -2232,7 +2236,7 @@ namespace MP.Data.Controllers public bool ST_CheckCleanByOdl(int idxOdl) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxOdl = new SqlParameter("@IdxOdl", idxOdl); @@ -2247,7 +2251,7 @@ namespace MP.Data.Controllers public bool ST_CheckUpsert(int idxOdl, int idxST, int oggetto, int num, string valueRead, string extCode, bool checkOk, string userMod, bool forced) { bool fatto = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxOdl = new SqlParameter("@IdxOdl", idxOdl); var IdxST = new SqlParameter("@IdxST", idxST); @@ -2276,7 +2280,7 @@ namespace MP.Data.Controllers public List STAR_byGrpOdl(string codGruppo, int idxODL) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var CodGruppo = new SqlParameter("@CodGruppo", codGruppo); var IdxODL = new SqlParameter("@IdxODL", idxODL); @@ -2301,7 +2305,7 @@ namespace MP.Data.Controllers public List STAR_byGrpOdlLbl(string codGruppo, string label, int idxODL) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var CodGruppo = new SqlParameter("@CodGruppo", codGruppo); var Label = new SqlParameter("@Label", label); @@ -2325,7 +2329,7 @@ namespace MP.Data.Controllers public List STAR_pendByOdl(int idxODL) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxODL = new SqlParameter("@IdxODL", idxODL); @@ -2347,7 +2351,7 @@ namespace MP.Data.Controllers public List StateMachineIngressi(int idxFam) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxFamIn = new SqlParameter("@IdxFamigliaIngresso", idxFam); dbResult = dbCtx @@ -2368,7 +2372,7 @@ namespace MP.Data.Controllers public StatoMacchineModel StatoMacchina(string idxMacchina) { StatoMacchineModel dbResult = new StatoMacchineModel(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetStatoMacc @@ -2388,7 +2392,7 @@ namespace MP.Data.Controllers public StatoProdModel StatoProdMacchina(string idxMacchina, DateTime dtReq) { StatoProdModel dbResult = new StatoProdModel(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var DataOra = new SqlParameter("@DataOra ", dtReq); @@ -2413,7 +2417,7 @@ namespace MP.Data.Controllers public List TemplateKitFilt(string KitCode, string codChild) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetTempKit @@ -2432,7 +2436,7 @@ namespace MP.Data.Controllers public TurniMaccModel TurnoMacchinaGet(string idxMacchina) { TurniMaccModel dbResult = new TurniMaccModel(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); dbResult = dbCtx @@ -2452,7 +2456,7 @@ namespace MP.Data.Controllers public List TurnoMacchinaGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetTurniMacc @@ -2471,7 +2475,7 @@ namespace MP.Data.Controllers public bool TurnoMacchinaToggle(string idxMacchina, int numTurno) { bool answ = false; - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacchina = new SqlParameter("@IdxMacchina", idxMacchina); var NumTurno = new SqlParameter("@numTurno", numTurno); @@ -2493,7 +2497,7 @@ namespace MP.Data.Controllers public List VocabolarioGetAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetVocabolario @@ -2511,7 +2515,7 @@ namespace MP.Data.Controllers public List VSCS_getAll() { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { dbResult = dbCtx .DbSetVSCS @@ -2530,7 +2534,7 @@ namespace MP.Data.Controllers public List VSOdlGetLastByMacc(string idxMacchina, int numRec) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacc = new SqlParameter("@IdxMacchina", idxMacchina); var NumRec = new SqlParameter("@numRec", numRec); @@ -2554,7 +2558,7 @@ namespace MP.Data.Controllers public List VSOdlGetUnused(string idxMacchina, bool showAll, int numDayAdd) { List dbResult = new List(); - using (var dbCtx = new MoonProContext(_configuration)) + using (var dbCtx = new MoonProContext(options)) { var IdxMacc = new SqlParameter("@IdxMacchina", idxMacchina); var ShowAll = new SqlParameter("@showAll", showAll); @@ -2574,8 +2578,8 @@ namespace MP.Data.Controllers #region Private Fields private static IConfiguration _configuration; - private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + private DbContextOptions options; #endregion Private Fields } diff --git a/MP.Data/DataServiceCollectionExtensions.cs b/MP.Data/DataServiceCollectionExtensions.cs index 40e538a9..78eb73e2 100644 --- a/MP.Data/DataServiceCollectionExtensions.cs +++ b/MP.Data/DataServiceCollectionExtensions.cs @@ -1,8 +1,10 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using MP.Data.Controllers; +using MP.Data.Repository.IOC; using MP.Data.Repository.Mtc; using MP.Data.Repository.Utils; +using MP.Data.Services.IOC; using MP.Data.Services.Mtc; using MP.Data.Services.Utils; @@ -25,6 +27,7 @@ namespace MP.Data services.TryAddSingleton(); // Repository Scoped + services.TryAddScoped(); services.TryAddScoped(); services.TryAddScoped(); @@ -33,6 +36,7 @@ namespace MP.Data services.TryAddSingleton(); // Servizi Scoped + services.TryAddScoped(); services.TryAddScoped(); services.TryAddScoped(); diff --git a/MP.Data/MP.Data.csproj b/MP.Data/MP.Data.csproj index 71cf1bc6..c1717ce8 100644 --- a/MP.Data/MP.Data.csproj +++ b/MP.Data/MP.Data.csproj @@ -37,7 +37,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/MP.Data/MoonProContext.cs b/MP.Data/MoonProContext.cs index 7c0d4658..0d65c590 100644 --- a/MP.Data/MoonProContext.cs +++ b/MP.Data/MoonProContext.cs @@ -22,10 +22,16 @@ namespace MP.Data #region Public Constructors - public MoonProContext(IConfiguration configuration) - { - _configuration = configuration; - } + //public MoonProContext(IConfiguration configuration) + //{ + // _configuration = configuration; + //} + + //public MoonProContext(DbContextOptions options, IConfiguration configuration) + //: base(options) + //{ + // _configuration = configuration; + //} public MoonProContext(DbContextOptions options) : base(options) { diff --git a/MP.Data/Repository/IOC/BaseRepository.cs b/MP.Data/Repository/IOC/BaseRepository.cs new file mode 100644 index 00000000..7814ee85 --- /dev/null +++ b/MP.Data/Repository/IOC/BaseRepository.cs @@ -0,0 +1,47 @@ +using Microsoft.EntityFrameworkCore; +using System.Threading.Tasks; + +namespace MP.Data.Repository.IOC +{ + public abstract class BaseRepository : IBaseRepository + { + #region Protected Fields + + protected readonly IDbContextFactory _ctxFactory; + + #endregion Protected Fields + + #region Protected Constructors + + protected BaseRepository(IDbContextFactory ctxFactory) + => _ctxFactory = ctxFactory; + + #endregion Protected Constructors + + #region Protected Methods + + /// + /// Creazione dbcontext per singola transazione + /// + /// + protected async Task CreateContextAsync() + => await _ctxFactory.CreateDbContextAsync(); + + #endregion Protected Methods + +#if false + /// + /// Salvataggio dati asincrono + /// + /// + protected async Task SaveChangesAsync(DataLayerContext ctx) + => await ctx.SaveChangesAsync() > 0; +#endif + +#if false + protected readonly DataLayerContext _dbCtx; + protected BaseRepository(DataLayerContext db) => _dbCtx = db; + public async Task SaveChangesAsync() => await _dbCtx.SaveChangesAsync() > 0; +#endif + } +} \ No newline at end of file diff --git a/MP.Data/Repository/IBaseRepository.cs b/MP.Data/Repository/IOC/IBaseRepository.cs similarity index 82% rename from MP.Data/Repository/IBaseRepository.cs rename to MP.Data/Repository/IOC/IBaseRepository.cs index 6e63d664..7a2feeb7 100644 --- a/MP.Data/Repository/IBaseRepository.cs +++ b/MP.Data/Repository/IOC/IBaseRepository.cs @@ -1,4 +1,4 @@ -namespace MP.Data.Repository +namespace MP.Data.Repository.IOC { public interface IBaseRepository { diff --git a/MP.Data/Repository/IOC/IIocRepository.cs b/MP.Data/Repository/IOC/IIocRepository.cs new file mode 100644 index 00000000..eb74fdde --- /dev/null +++ b/MP.Data/Repository/IOC/IIocRepository.cs @@ -0,0 +1,39 @@ +using MP.Data.DbModels; +using System; +using System.Threading.Tasks; +using static MP.Core.Objects.Enums; + +namespace MP.Data.Repository.IOC +{ + /// + /// Interfaccia per metodi IOC + /// + public interface IIocRepository + { + #region Public Methods + + /// + /// Processing intera catena eventi verifica cambio stato in singola transazione e con unico DbContext + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + Task CheckCambiaStatoBatchAsync(tipoInputEvento tipoInput, string IdxMacchina, DateTime InizioStato, int IdxTipo, string CodArt, string Value, int MatrOpr, string pallet); + + /// + /// Aggiunta record MicroStato + EventList + /// + /// + /// + /// + Task EvListMicroStatoInsertAsync(MicroStatoMacchinaModel newRecMsm, EventListModel newRecEv); + + #endregion Public Methods + } +} \ No newline at end of file diff --git a/MP.Data/Repository/IOC/IocRepository.cs b/MP.Data/Repository/IOC/IocRepository.cs new file mode 100644 index 00000000..e1fbc0a5 --- /dev/null +++ b/MP.Data/Repository/IOC/IocRepository.cs @@ -0,0 +1,155 @@ +using Microsoft.Data.SqlClient; +using Microsoft.EntityFrameworkCore; +using MP.Data.DbModels; +using NLog; +using System; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using static MP.Core.Objects.Enums; + +namespace MP.Data.Repository.IOC +{ + public class IocRepository : BaseRepository, IIocRepository + { + #region Public Constructors + + public IocRepository(IDbContextFactory ctxFactory) : base(ctxFactory) + { + } + + #endregion Public Constructors + + #region Public Methods + + /// + public async Task CheckCambiaStatoBatchAsync(tipoInputEvento tipoInput, string IdxMacchina, DateTime InizioStato, int IdxTipo, string CodArt, string Value, int MatrOpr, string pallet) + { + await using var dbCtx = await CreateContextAsync(); + await using var tx = await dbCtx.Database.BeginTransactionAsync(IsolationLevel.ReadCommitted); + //await using var tx = await dbCtx.Database.BeginTransactionAsync(); + + try + { + var pIdxMacchina = new SqlParameter("@IdxMacchina", IdxMacchina); + var pIdxTipo = new SqlParameter("@IdxTipo", IdxTipo); + var pInizioStato = new SqlParameter("@InizioStato", InizioStato); + var pCodArticolo = new SqlParameter("@codArticolo", CodArt); + var pValue = new SqlParameter("@Value", Value); + var pMatrOpr = new SqlParameter("@MatrOpr", MatrOpr); + var pPallet = new SqlParameter("@pallet", pallet); + + string opType = tipoInput switch + { + tipoInputEvento.barcode => "BARCODE", + tipoInputEvento.hw => "HW", + _ => "ND" + }; + + TransizioneStatiModel? rigaTrans = null; + + if (opType != "ND") + { + string sql = tipoInput switch + { + tipoInputEvento.barcode => "EXEC dbo.stp_TS_getUserForcedTrans @IdxMacchina, @IdxTipo", + tipoInputEvento.hw => "EXEC dbo.stp_TS_getByIdxMacchIdxTipoEv @IdxMacchina, @IdxTipo", + _ => string.Empty + }; + + // ✅ prima chiamata ASYNC + rigaTrans = (await dbCtx.DbSetSMES + .FromSqlRaw(sql, pIdxMacchina, pIdxTipo) + .AsNoTracking() + .ToListAsync() + ).FirstOrDefault(); + } + + if (rigaTrans != null && rigaTrans.IdxStato != rigaTrans.next_IdxStato) + { + var pIdxStato = new SqlParameter("@IdxStato", rigaTrans.next_IdxStato); + + await dbCtx.Database.ExecuteSqlRawAsync( + "EXEC dbo.stp_DDB_InsStatoBatch @IdxMacchina, @InizioStato, @IdxStato, @codArticolo, @Value, @MatrOpr, @pallet", + pIdxMacchina, pInizioStato, pIdxStato, pCodArticolo, pValue, pMatrOpr, pPallet); + + // eseguo solo se evento manuale/barcode + if (tipoInput == tipoInputEvento.barcode) + { + var pMaxAgeSec = new SqlParameter("@maxAgeSec", 0); + await dbCtx.Database.ExecuteSqlRawAsync( + "EXEC stp_MSE_recalc @maxAgeSec, @idxMacchina", + pMaxAgeSec, pIdxMacchina); + } + } + else + { + Log.Debug($"Nessun cambio stato richiesto | {opType} | {IdxMacchina} | {IdxTipo}"); + } + + // Nessuna eccezione = successo + await tx.CommitAsync(); + return true; + } + catch (Exception ex) + { + await tx.RollbackAsync(); + // Log dettagliato errore + Log.Error(ex, $"Errore in CheckCambiaStatoBatchAsync: {IdxMacchina} | {tipoInput}"); + throw; // O return false; se il chiamante gestisce fallimenti + } + } + + /// + public async Task EvListMicroStatoInsertAsync(MicroStatoMacchinaModel newRecMsm, EventListModel newRecEv) + { + // eseguo in transazione... + await using var dbCtx = await CreateContextAsync(); + await using var tx = await dbCtx.Database.BeginTransactionAsync(IsolationLevel.ReadCommitted); + + try + { + // inizio con record microstato... + var actRec = await dbCtx + .DbSetMicroStatoMacc + .FindAsync(newRecMsm.IdxMacchina); + //.SingleOrDefaultAsync(); + if (actRec == null) + { + dbCtx.DbSetMicroStatoMacc.Add(newRecMsm); + } + else + { + actRec.IdxMicroStato = newRecMsm.IdxMicroStato; + actRec.InizioStato = newRecMsm.InizioStato; + actRec.Value = newRecMsm.Value; + + // Update() allega l'entità e segna tutti i campi come Modified + dbCtx.DbSetMicroStatoMacc.Update(actRec); + } + + // ora record EVList + dbCtx.DbSetEvList.Add(newRecEv); + + // EF Core 8 raggruppa automaticamente INSERT/UPDATE in un batch + var rowsAffected = await dbCtx.SaveChangesAsync(); + await tx.CommitAsync(); + + return rowsAffected > 0; + } + catch + { + await tx.RollbackAsync(); + throw; + } + } + + #endregion Public Methods + + #region Protected Fields + + protected static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + + #endregion Protected Fields + } +} \ No newline at end of file diff --git a/MP.Data/Repository/Mtc/MtcSetupRepository.cs b/MP.Data/Repository/Mtc/MtcSetupRepository.cs index 4912f651..b391d3a4 100644 --- a/MP.Data/Repository/Mtc/MtcSetupRepository.cs +++ b/MP.Data/Repository/Mtc/MtcSetupRepository.cs @@ -1,5 +1,6 @@ using Microsoft.EntityFrameworkCore; using MP.Data.DbModels.Mtc; +using MP.Data.Repository.Utils; using System.Threading.Tasks; namespace MP.Data.Repository.Mtc diff --git a/MP.Data/Repository/BaseRepository.cs b/MP.Data/Repository/Utils/BaseRepository.cs similarity index 97% rename from MP.Data/Repository/BaseRepository.cs rename to MP.Data/Repository/Utils/BaseRepository.cs index 7afc1bd2..4e709040 100644 --- a/MP.Data/Repository/BaseRepository.cs +++ b/MP.Data/Repository/Utils/BaseRepository.cs @@ -1,7 +1,7 @@ using Microsoft.EntityFrameworkCore; using System.Threading.Tasks; -namespace MP.Data.Repository +namespace MP.Data.Repository.Utils { public abstract class BaseRepository : IBaseRepository { diff --git a/MP.Data/Repository/Utils/IBaseRepository.cs b/MP.Data/Repository/Utils/IBaseRepository.cs new file mode 100644 index 00000000..c6a4988a --- /dev/null +++ b/MP.Data/Repository/Utils/IBaseRepository.cs @@ -0,0 +1,8 @@ +namespace MP.Data.Repository.Utils +{ + public interface IBaseRepository + { + //Task CreateContextAsync(); + //Task SaveChangesAsync(DataLayerContext ctx); + } +} diff --git a/MP.Data/Services/IOC/IIocService.cs b/MP.Data/Services/IOC/IIocService.cs new file mode 100644 index 00000000..f33a9942 --- /dev/null +++ b/MP.Data/Services/IOC/IIocService.cs @@ -0,0 +1,33 @@ +using MP.Data.DbModels; +using System; +using System.Threading.Tasks; +using static MP.Core.Objects.Enums; + +namespace MP.Data.Services.IOC +{ + public interface IIocService + { + + /// + /// Processing intera catena eventi verifica cambio stato in singola transazione e con unico DbContext + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + Task CheckCambiaStatoBatchAsync(tipoInputEvento tipoInput, string IdxMacchina, DateTime InizioStato, int IdxTipo, string CodArt, string Value, int MatrOpr, string pallet); + + /// + /// Aggiunta record MicroStato + EventList + /// + /// + /// + /// + Task EvListMicroStatoInsertAsync(MicroStatoMacchinaModel newRecMsm, EventListModel newRecEv); + } +} diff --git a/MP.Data/Services/IOC/IocService.cs b/MP.Data/Services/IOC/IocService.cs new file mode 100644 index 00000000..5ed6e26c --- /dev/null +++ b/MP.Data/Services/IOC/IocService.cs @@ -0,0 +1,59 @@ +using Microsoft.Extensions.Configuration; +using MP.Data.DbModels; +using MP.Data.Repository.IOC; +using StackExchange.Redis; +using System; +using System.Threading.Tasks; +using static MP.Core.Objects.Enums; + +namespace MP.Data.Services.IOC +{ + public class IocService : BaseServ, IIocService + { + + private readonly string _className; + private readonly IIocRepository _repo; + + public IocService( + IConfiguration config, + IConnectionMultiplexer redis, + IIocRepository repo) : base(config, redis) + { + _className = "IocServ"; + _repo = repo; + } + + /// + public async Task CheckCambiaStatoBatchAsync(tipoInputEvento tipoInput, string IdxMacchina, DateTime InizioStato, int IdxTipo, string CodArt, string Value, int MatrOpr, string pallet) + { + return await TraceAsync($"{_className}.CheckCambiaStatoBatch", async (activity) => + { + // eseguo su repostory (DB) + string operation = "UPDATE"; + bool success = await _repo.CheckCambiaStatoBatchAsync(tipoInput, IdxMacchina, InizioStato, IdxTipo, CodArt, Value, MatrOpr, pallet); + + activity?.SetTag("db.operation", operation); + + await ClearCacheAsync($"{_redisBaseKey}:{_className}:*"); + return success; + }); + } + + + /// + public async Task EvListMicroStatoInsertAsync(MicroStatoMacchinaModel newRecMsm, EventListModel newRecEv) + { + return await TraceAsync($"{_className}.EvListMicroStatoInsert", async (activity) => + { + // eseguo su repostory (DB) + string operation = "UPDATE"; + bool success = await _repo.EvListMicroStatoInsertAsync(newRecMsm, newRecEv); + + activity?.SetTag("db.operation", operation); + + await ClearCacheAsync($"{_redisBaseKey}:{_className}:*"); + return success; + }); + } + } +} diff --git a/MP.IOC/Data/MpDataService.cs b/MP.IOC/Data/MpDataService.cs index f5ddf73b..d67d3f07 100644 --- a/MP.IOC/Data/MpDataService.cs +++ b/MP.IOC/Data/MpDataService.cs @@ -7,6 +7,7 @@ using MP.Data.Controllers; using MP.Data.DbModels; using MP.Data.DbModels.Anag; using MP.Data.MgModels; +using MP.Data.Services.IOC; using MP.Data.Services.Mtc; using Newtonsoft.Json; using NLog; @@ -988,16 +989,6 @@ namespace MP.IOC.Data int idxMicroStato = Convert.ToInt32(datiMacc["IdxMicroStato"]); int valIOB = Convert.ToInt32(valINT); next_idxMS = idxMicroStato; -#if false - // verifico esistenza tab SMI... - var fiHASH = Utils.GetHashSMI(idxFamIn); - bool trovato = await RedisKeyPresentAsync(fiHASH); - if (!trovato) - { - // ricarico tabella (salvando in redis x ricerca successiva)! - KeyValuePair[] valori = await StateMachInByKeyAsync(idxFamIn); - } -#endif // recupero singolo valOut (stringa) x chiave string todoSMI = await ValoreSmiAsync(idxFamIn, idxMicroStato, valIOB); // solo se ho trovato un risultato nella tab SMI della famiglia macchina... @@ -4816,16 +4807,31 @@ namespace MP.IOC.Data private async Task scriviRigaEventoAsync(MicroStatoMacchinaModel newRecMsm, EventListModel newRecEv) { bool inserito = false; - try + if (useFactory) { + await using var scope = _scopeFactory.CreateAsyncScope(); + var iocService = scope.ServiceProvider.GetRequiredService(); + // inserisco evento - inserito = await IocDbController.EvListMicroStatoInsertAsync(newRecMsm, newRecEv); + inserito = await iocService.EvListMicroStatoInsertAsync(newRecMsm, newRecEv); // faccio controllo per eventuale cambio stato da tab transizioni... - await CheckCambiaStatoBatchAsync(tipoInputEvento.hw, newRecEv.IdxMacchina, newRecEv.InizioStato ?? DateTime.Now, newRecEv.IdxTipo, newRecEv.CodArticolo, newRecEv.Value, newRecEv.MatrOpr, newRecEv.pallet); + + await iocService.CheckCambiaStatoBatchAsync(tipoInputEvento.hw, newRecEv.IdxMacchina, newRecEv.InizioStato ?? DateTime.Now, newRecEv.IdxTipo, newRecEv.CodArticolo, newRecEv.Value, newRecEv.MatrOpr, newRecEv.pallet); } - catch (Exception exc) + else { - Log.Error($"Errore in scriviRigaEvento | IdxMacchina {newRecEv.IdxMacchina} | IdxTipo {newRecEv.IdxTipo} | codArticolo {newRecEv.CodArticolo} | Value {newRecEv.Value} | MatrOpr {newRecEv.MatrOpr} | Pallet {newRecEv.pallet} | dTime {newRecEv.InizioStato}{Environment.NewLine}{exc}"); + + try + { + // inserisco evento + inserito = await IocDbController.EvListMicroStatoInsertAsync(newRecMsm, newRecEv); + // faccio controllo per eventuale cambio stato da tab transizioni... + await CheckCambiaStatoBatchAsync(tipoInputEvento.hw, newRecEv.IdxMacchina, newRecEv.InizioStato ?? DateTime.Now, newRecEv.IdxTipo, newRecEv.CodArticolo, newRecEv.Value, newRecEv.MatrOpr, newRecEv.pallet); + } + catch (Exception exc) + { + Log.Error($"Errore in scriviRigaEvento | IdxMacchina {newRecEv.IdxMacchina} | IdxTipo {newRecEv.IdxTipo} | codArticolo {newRecEv.CodArticolo} | Value {newRecEv.Value} | MatrOpr {newRecEv.MatrOpr} | Pallet {newRecEv.pallet} | dTime {newRecEv.InizioStato}{Environment.NewLine}{exc}"); + } } // formatto output inputComandoMapo answ = new inputComandoMapo(); diff --git a/MP.IOC/MP.IOC.csproj b/MP.IOC/MP.IOC.csproj index e138c6a2..c6c46bde 100644 --- a/MP.IOC/MP.IOC.csproj +++ b/MP.IOC/MP.IOC.csproj @@ -4,7 +4,7 @@ net8.0 enable enable - 8.16.2604.3008 + 8.16.2604.3015 diff --git a/MP.IOC/Program.cs b/MP.IOC/Program.cs index e665ee1f..207578c7 100644 --- a/MP.IOC/Program.cs +++ b/MP.IOC/Program.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Http.Extensions; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.OpenApi.Models; using MP.Core.Conf; using MP.Data; @@ -62,6 +63,20 @@ builder.Services.Configure( builder.Configuration.GetSection("RedisScripts")); logger.Info("RedisScript Provider configured"); +// Metodi principali x accesso dati +var connStr = builder.Configuration.GetConnectionString("MP.Data") + ?? throw new InvalidOperationException("ConnString 'MP.Data' mancante."); + +builder.Services.AddDbContextFactory(options => + options.UseSqlServer(connStr) + .EnableSensitiveDataLogging(false) // true solo in Sviluppo + .ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning))); + +// MP.Data DbContext for Stats repositories +string utilsConnString = builder.Configuration.GetConnectionString("MP.Utils") ?? "Server=localhost;Database=MoonPro_Utils; integrated security=True; MultipleActiveResultSets=True; App=MP.IOC;"; +builder.Services.AddDbContextFactory(options => + options.UseSqlServer(utilsConnString)); + // MP.Data Services Utils - Statistiche DB builder.Services.AddIocDataLayer(); @@ -79,10 +94,7 @@ builder.Services.AddRazorComponents() //builder.Services.AddHealthChecks() // .AddSqlServer(builder.Configuration.GetConnectionString("CoreDb")); -// MP.Data DbContext for Stats repositories -string utilsConnString = builder.Configuration.GetConnectionString("MP.Utils") ?? "Server=localhost;Database=MoonPro_Utils; integrated security=True; MultipleActiveResultSets=True; App=MP.IOC;"; -builder.Services.AddDbContextFactory(options => - options.UseSqlServer(utilsConnString)); + // generic controller builder.Services.AddControllers(); @@ -104,6 +116,7 @@ var redisMux = ConnectionMultiplexer.Connect(confRedis); builder.Services.AddSingleton(redisMux); // oggetto principale accesso dati +//builder.Services.AddScoped(); builder.Services.AddSingleton(); logger.Info("Standard service configured"); diff --git a/MP.IOC/Resources/ChangeLog.html b/MP.IOC/Resources/ChangeLog.html index f1e2e693..18046f4d 100644 --- a/MP.IOC/Resources/ChangeLog.html +++ b/MP.IOC/Resources/ChangeLog.html @@ -1,6 +1,6 @@ Modulo MP-IOC -

Versione: 8.16.2604.3008

+

Versione: 8.16.2604.3015


Note di rilascio:
  • diff --git a/MP.IOC/Resources/VersNum.txt b/MP.IOC/Resources/VersNum.txt index 6108c928..464e39a8 100644 --- a/MP.IOC/Resources/VersNum.txt +++ b/MP.IOC/Resources/VersNum.txt @@ -1 +1 @@ -8.16.2604.3008 +8.16.2604.3015 diff --git a/MP.IOC/Resources/manifest.xml b/MP.IOC/Resources/manifest.xml index a7c4baf3..18c769d4 100644 --- a/MP.IOC/Resources/manifest.xml +++ b/MP.IOC/Resources/manifest.xml @@ -1,6 +1,6 @@ - 8.16.2604.3008 + 8.16.2604.3015 https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/MP.IOC.zip https://nexus.steamware.net/repository/SWS/MP-IOC/stable/LAST/ChangeLog.html false