diff --git a/LiMan.DB/Controllers/DbController.cs b/LiMan.DB/Controllers/DbController.cs index 434cad7..ff9bf0f 100644 --- a/LiMan.DB/Controllers/DbController.cs +++ b/LiMan.DB/Controllers/DbController.cs @@ -342,11 +342,35 @@ namespace LiMan.DB.Controllers { if (licenza.IsValid && (licenza.NumLicenze >= (attivList.Count + ParamDict.Count))) { - DateTime vetoDate = DateTime.Today.AddDays(numDayVeto); - using (LMDbContext localDbCtx = new LMDbContext(_configuration)) + DateTime oggi = DateTime.Today; + DateTime vetoDate = oggi.AddDays(numDayVeto); + using (LMDbContext dbCtx = new LMDbContext(_configuration)) { try { + // cerco eventuali licenze preesistenti (per CodImp) e le elimino SE sono libere... + List list2rem = new List(); + foreach (var item in ParamDict) + { + var rec2del = dbCtx + .DbSetSubLicenze + .Where(x => x.CodImpiego == item.Key && x.VetoUnlock <= oggi) + .FirstOrDefault(); + if(rec2del!=null) + { + list2rem.Add(rec2del); + } + } + // se ce ne sono rimuovo e salvo... + if(list2rem!=null && list2rem.Count>0) + { + dbCtx + .DbSetSubLicenze + .RemoveRange(list2rem); + + dbCtx.SaveChanges(); + } + // genero subLicenze... var newRec = ParamDict .Select(x => new SubLicenzaModel() @@ -358,11 +382,11 @@ namespace LiMan.DB.Controllers Chiave = x.Value }) .ToList(); - localDbCtx + dbCtx .DbSetSubLicenze .AddRange(newRec); - localDbCtx.SaveChanges(); + dbCtx.SaveChanges(); } catch (Exception exc) {