using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using NLog; using SMGen.Data.DbModels; namespace SMGen.Data.Controllers { public class SMGenController : IDisposable { private static IConfiguration _configuration; private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); public SMGenController(IConfiguration configuration) { _configuration = configuration; } public void Dispose() { } /// /// Aggiunta in bulk delle transizioni/ingressi /// /// /// public async Task TranInInsert(List newTIRecs, int currIdxFamiglia) { bool fatto = false; using (SMGDataContext localDbCtx = new SMGDataContext(_configuration)) { try { foreach (var item in newTIRecs) { var currRec = localDbCtx .DbSetTranIngTemp .Where(x => (x.IdxFamigliaIngresso == item.IdxFamigliaIngresso) && (x.IdxMicroStato == item.IdxMicroStato) && (x.ValoreIngresso == item.ValoreIngresso)) .FirstOrDefault(); if (currRec != null) { currRec.IdxTipoEvento = item.IdxTipoEvento; currRec.next_IdxMicroStato = item.next_IdxMicroStato; localDbCtx.Entry(currRec).State = EntityState.Modified; } else { localDbCtx .DbSetTranIngTemp .Add(item); } } await localDbCtx.SaveChangesAsync(); fatto = true; Log.Info($"Scritto su db idxFamiglia: {currIdxFamiglia} "); } catch (Exception exc) { Log.Error($"Eccezione durante TranInInsert ({currIdxFamiglia}) : {Environment.NewLine}{exc}"); } } return fatto; } /// /// Aggiunta in bulk delle transizioni/ingressi /// /// /// public async Task AnagEventinInsert(List newEvRecs) { bool fatto = false; using (SMGDataContext localDbCtx = new SMGDataContext(_configuration)) { try { //localDbCtx // .DbSetAnagEventiTemp // .AddRange(newEvRecs); //await localDbCtx.SaveChangesAsync(); //fatto = true; foreach (var item in newEvRecs) { var currRec = localDbCtx .DbSetAnagEventiTemp .Where(x => (x.IdxTipo == item.IdxTipo)) .FirstOrDefault(); if (currRec != null) { currRec.Nome = item.Nome; localDbCtx.Entry(currRec).State = EntityState.Modified; } else { localDbCtx .DbSetAnagEventiTemp .Add(item); } } await localDbCtx.SaveChangesAsync(); fatto = true; Log.Info($"Gli stati sono OK!"); } catch (Exception exc) { Log.Error($"Eccezione durante AnagEventinInsert: {Environment.NewLine}{exc}"); } } return fatto; } } }