Merge branch 'release/FixDbCOntext'

This commit is contained in:
Samuele Locatelli
2021-09-29 14:06:08 +02:00
11 changed files with 238 additions and 181 deletions
+233 -161
View File
@@ -15,7 +15,6 @@ namespace GWMS.Data.Controllers
#region Private Fields
private static IConfiguration _configuration;
private static GWMSContext dbCtx;
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
@@ -25,8 +24,6 @@ namespace GWMS.Data.Controllers
public GWMSController(IConfiguration configuration)
{
_configuration = configuration;
dbCtx = new GWMSContext(configuration);
//Log.Info("Avviata classe GWMSController");
}
#endregion Public Constructors
@@ -49,14 +46,17 @@ namespace GWMS.Data.Controllers
public bool DbForceMigrate()
{
bool answ = false;
try
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
dbCtx.DbForceMigrate();
answ = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in DbForceMigrate");
try
{
localDbCtx.DbForceMigrate();
answ = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in DbForceMigrate");
}
}
return answ;
}
@@ -64,25 +64,30 @@ namespace GWMS.Data.Controllers
public void Dispose()
{
// Clear database context
dbCtx.Dispose();
//Log.Info("Dispose di GWMSController");
}
public List<ConfigModel> GetConfig()
{
var dbResult = dbCtx
List<ConfigModel> dbResult = new List<ConfigModel>();
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
dbResult = localDbCtx
.DbSetConfig
.ToList();
}
return dbResult;
}
public List<ItemModel> GetItems()
{
var dbResult = dbCtx
List<ItemModel> dbResult = new List<ItemModel>();
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
dbResult = localDbCtx
.DbSetItems
.ToList();
}
return dbResult;
}
@@ -120,7 +125,10 @@ namespace GWMS.Data.Controllers
public List<OrderModel> GetOrdersFilt(int PlantId, int SupplierId, int TransporterId, DateTime DtStart, DateTime DtEnd, bool ShowClosed)
{
var dbResult = dbCtx
List<OrderModel> dbResult = new List<OrderModel>();
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
dbResult = localDbCtx
.DbSetOrders
.Where(x => (x.PlantId == PlantId || PlantId == 0) && (x.SupplierId == SupplierId || SupplierId == 0) && (x.TransporterId == TransporterId || TransporterId == 0) && (x.DtOrder >= DtStart && x.DtOrder <= DtEnd) && (x.ExecutionQty == 0 || ShowClosed))
.Include(p => p.Plant)
@@ -128,13 +136,16 @@ namespace GWMS.Data.Controllers
.Include(t => t.Transporter)
.OrderByDescending(x => x.DtOrder)
.ToList();
}
return dbResult;
}
public List<OrderModel> GetOrdersOpen(int PlantId)
{
var dbResult = dbCtx
List<OrderModel> dbResult = new List<OrderModel>();
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
dbResult = localDbCtx
.DbSetOrders
.Where(x => (x.PlantId == PlantId || PlantId == 0) && (x.DtExecStart < x.DtOrder))
.Include(p => p.Plant)
@@ -142,17 +153,20 @@ namespace GWMS.Data.Controllers
.Include(t => t.Transporter)
.OrderByDescending(x => x.DtOrder)
.ToList();
}
return dbResult;
}
public PlantDetailModel GetPlant(int PlantId)
{
var dbResult = dbCtx
PlantDetailModel dbResult = new PlantDetailModel();
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
dbResult = localDbCtx
.DbSetPlant
.Where(x => x.PlantId == PlantId)
.FirstOrDefault();
}
return dbResult;
}
@@ -165,13 +179,16 @@ namespace GWMS.Data.Controllers
/// <returns></returns>
public List<PlantLogModel> GetPlantLog(int PlantId, DateTime DtMaxDate, int numRec)
{
var dbResult = dbCtx
List<PlantLogModel> dbResult = new List<PlantLogModel>();
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
dbResult = localDbCtx
.DbSetPlantLog
.Where(x => (x.PlantId == PlantId || PlantId == 0) && x.DtEvent <= DtMaxDate)
.OrderByDescending(x => x.DtEvent)
.Take(numRec)
.ToList();
}
return dbResult;
}
@@ -273,10 +290,13 @@ namespace GWMS.Data.Controllers
public List<PlantDetailModel> GetPlants()
{
var dbResult = dbCtx
List<PlantDetailModel> dbResult = new List<PlantDetailModel>();
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
dbResult = localDbCtx
.DbSetPlant
.ToList();
}
return dbResult;
}
@@ -304,46 +324,58 @@ namespace GWMS.Data.Controllers
/// <returns></returns>
public List<RebootLogModel> GetRebootLog(int maxNum = 100)
{
if (maxNum == 0)
List<RebootLogModel> dbResult = new List<RebootLogModel>();
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
maxNum = dbCtx.DbRebootLog.Count();
if (maxNum == 0)
{
maxNum = localDbCtx.DbRebootLog.Count();
}
dbResult = localDbCtx
.DbRebootLog
.OrderByDescending(x => x.DtEvent)
.Take(maxNum)
.ToList();
}
var dbResult = dbCtx
.DbRebootLog
.OrderByDescending(x => x.DtEvent)
.Take(maxNum)
.ToList();
return dbResult;
}
public List<SupplierModel> GetSuppliers()
{
var dbResult = dbCtx
List<SupplierModel> dbResult = new List<SupplierModel>();
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
dbResult = localDbCtx
.DbSetSupplier
.ToList();
}
return dbResult;
}
public List<TransporterModel> GetTransporters()
{
var dbResult = dbCtx
List<TransporterModel> dbResult = new List<TransporterModel>();
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
dbResult = localDbCtx
.DbSetTransporter
.ToList();
}
return dbResult;
}
public List<UserModel> GetUsersFilt(UserLevel UsrLvl, int PlantId, int SupplierId, int TransporterId)
{
var dbResult = dbCtx
List<UserModel> dbResult = new List<UserModel>();
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
dbResult = localDbCtx
.DbSetUser
.Where(x => (x.Livello == UsrLvl || UsrLvl == UserLevel.ND) && (x.MaskPlantId == PlantId || PlantId == 0) && (x.MaskSupplierId == SupplierId || SupplierId == 0) && (x.MaskTranspId == TransporterId || TransporterId == 0))
.OrderBy(x => x.Livello)
.ThenBy(x => x.UserName)
.ToList();
}
return dbResult;
}
@@ -365,15 +397,18 @@ namespace GWMS.Data.Controllers
public bool HasPlantLog()
{
bool answ = false;
try
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
var numRecord = dbCtx
.DbSetPlantLog
.Count();
answ = numRecord > 0;
try
{
var numRecord = localDbCtx
.DbSetPlantLog
.Count();
answ = numRecord > 0;
}
catch
{ }
}
catch
{ }
return answ;
}
@@ -385,24 +420,27 @@ namespace GWMS.Data.Controllers
public bool OrderDelete(OrderModel Item2Del)
{
bool done = false;
try
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
if (Item2Del != null)
try
{
var rec2del = dbCtx
.DbSetOrders
.Where(x => x.OrderId == Item2Del.OrderId)
.FirstOrDefault();
dbCtx
.DbSetOrders
.Remove(rec2del);
dbCtx.SaveChanges();
done = true;
if (Item2Del != null)
{
var rec2del = localDbCtx
.DbSetOrders
.Where(x => x.OrderId == Item2Del.OrderId)
.FirstOrDefault();
localDbCtx
.DbSetOrders
.Remove(rec2del);
localDbCtx.SaveChanges();
done = true;
}
}
catch (Exception exc)
{
Log.Error($"Eccezione in OrderDelete:{Environment.NewLine}{exc}");
}
}
catch (Exception exc)
{
Log.Error($"Eccezione in OrderDelete:{Environment.NewLine}{exc}");
}
return done;
}
@@ -415,16 +453,19 @@ namespace GWMS.Data.Controllers
public bool OrderInsert(List<OrderModel> newItems)
{
bool done = false;
try
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
dbCtx
.DbSetOrders
.AddRange(newItems);
dbCtx.SaveChanges();
done = true;
try
{
localDbCtx
.DbSetOrders
.AddRange(newItems);
localDbCtx.SaveChanges();
done = true;
}
catch (Exception exc)
{ }
}
catch (Exception exc)
{ }
return done;
}
@@ -436,16 +477,16 @@ namespace GWMS.Data.Controllers
public bool OrderUpdate(OrderModel updItem)
{
bool done = false;
try
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
OrderModel currData = null;
currData = dbCtx
.DbSetOrders
.Where(x => x.OrderId == updItem.OrderId)
.FirstOrDefault();
if (currData != null)
try
{
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
OrderModel currData = null;
currData = localDbCtx
.DbSetOrders
.Where(x => x.OrderId == updItem.OrderId)
.FirstOrDefault();
if (currData != null)
{
// se ho modificato data --> cambio codice ordine!
if (!localDbCtx.Entry(updItem).OriginalValues["DtOrder"].Equals(localDbCtx.Entry(updItem).CurrentValues["DtOrder"]))
@@ -456,19 +497,19 @@ namespace GWMS.Data.Controllers
localDbCtx.Entry(updItem).State = EntityState.Modified;
localDbCtx.SaveChanges();
}
else
{
localDbCtx
.DbSetOrders
.Add(updItem);
localDbCtx.SaveChanges();
}
done = true;
}
else
catch (Exception exc)
{
dbCtx
.DbSetOrders
.Add(updItem);
dbCtx.SaveChanges();
Log.Error($"Eccezione in OrderUpdate:{Environment.NewLine}{exc}");
}
done = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in OrderUpdate:{Environment.NewLine}{exc}");
}
return done;
}
@@ -591,7 +632,10 @@ namespace GWMS.Data.Controllers
/// <returns></returns>
public List<PlantLogModel> PlantLogGetLastByFlux(int PlantId)
{
List<PlantLogModel> lastRec = dbCtx
List<PlantLogModel> lastRec = new List<PlantLogModel>();
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
lastRec = localDbCtx
.DbSetPlantLog
.Where(x => x.PlantId == PlantId)
.OrderByDescending(o => o.DtEvent)
@@ -600,6 +644,7 @@ namespace GWMS.Data.Controllers
.GroupBy(g => g.FluxType)
.Select(s => s.First())
.ToList();
}
return lastRec;
}
@@ -611,43 +656,45 @@ namespace GWMS.Data.Controllers
public bool PlantLogInsertNew(List<PlantLogModel> newItems)
{
bool fatto = false;
try
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
dbCtx
.DbSetPlantLog
.AddRange(newItems);
dbCtx.SaveChanges();
fatto = true;
try
{
localDbCtx
.DbSetPlantLog
.AddRange(newItems);
localDbCtx.SaveChanges();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante PlantLogInsertNew per {newItems.Count} rec{Environment.NewLine}{exc}");
}
}
catch (Exception exc)
{
Log.Error(exc, $"Eccezione durante PlantLogInsertNew per {newItems.Count} rec");
}
return fatto;
}
public bool RecordRebootLog(RebootLogModel newItem)
{
bool done = false;
try
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
dbCtx
.DbRebootLog
.Add(newItem);
dbCtx.SaveChanges();
done = true;
try
{
localDbCtx
.DbRebootLog
.Add(newItem);
localDbCtx.SaveChanges();
done = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante RecordRebootLog{Environment.NewLine}{exc}");
}
}
catch (Exception exc)
{ }
return done;
}
public void ResetController()
{
dbCtx = new GWMSContext(_configuration);
}
/// <summary>
/// Annulla modifiche su una specifica entity (cancel update)
/// </summary>
@@ -656,16 +703,19 @@ namespace GWMS.Data.Controllers
public bool rollBackEntity(object item)
{
bool answ = false;
try
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
if (dbCtx.Entry(item).State == EntityState.Deleted || dbCtx.Entry(item).State == EntityState.Modified)
try
{
dbCtx.Entry(item).Reload();
if (localDbCtx.Entry(item).State == EntityState.Deleted || localDbCtx.Entry(item).State == EntityState.Modified)
{
localDbCtx.Entry(item).Reload();
}
}
catch (Exception exc)
{
Log.Error($"Eccezione in rollBackEntity{Environment.NewLine}{exc}");
}
}
catch (Exception exc)
{
Log.Error($"Eccezione in rollBackEntity{Environment.NewLine}{exc}");
}
return answ;
}
@@ -678,27 +728,32 @@ namespace GWMS.Data.Controllers
public bool UserUpdate(UserModel updItem)
{
bool done = false;
try
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
var currData = dbCtx
.DbSetUser
.Where(x => x.UserId == updItem.UserId)
.FirstOrDefault();
if (currData != null)
try
{
dbCtx.Entry(updItem).State = EntityState.Modified;
}
else
{
dbCtx
var currData = localDbCtx
.DbSetUser
.Add(updItem);
.Where(x => x.UserId == updItem.UserId)
.FirstOrDefault();
if (currData != null)
{
localDbCtx.Entry(updItem).State = EntityState.Modified;
}
else
{
localDbCtx
.DbSetUser
.Add(updItem);
}
localDbCtx.SaveChanges();
done = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante UserUpdate{Environment.NewLine}{exc}");
}
dbCtx.SaveChanges();
done = true;
}
catch (Exception exc)
{ }
return done;
}
@@ -710,18 +765,23 @@ namespace GWMS.Data.Controllers
public bool WeekPlanDelete(WeekPlanModel selRecord)
{
bool done = false;
try
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
var item2del = dbCtx
.DbSetPlantSupplWeekPlan
.Where(x => x.WeekPlanId == selRecord.WeekPlanId)
.FirstOrDefault();
dbCtx.DbSetPlantSupplWeekPlan.Remove(item2del);
dbCtx.SaveChanges();
done = true;
try
{
var item2del = localDbCtx
.DbSetPlantSupplWeekPlan
.Where(x => x.WeekPlanId == selRecord.WeekPlanId)
.FirstOrDefault();
localDbCtx.DbSetPlantSupplWeekPlan.Remove(item2del);
localDbCtx.SaveChanges();
done = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante WeekPlanDelete{Environment.NewLine}{exc}");
}
}
catch (Exception exc)
{ }
return done;
}
@@ -733,27 +793,39 @@ namespace GWMS.Data.Controllers
public bool WeekPlanUpdate(WeekPlanModel updItem)
{
bool done = false;
try
using (GWMSContext localDbCtx = new GWMSContext(_configuration))
{
var currData = dbCtx
.DbSetPlantSupplWeekPlan
.Where(x => x.WeekPlanId == updItem.WeekPlanId)
.FirstOrDefault();
if (currData != null)
try
{
dbCtx.Entry(updItem).State = EntityState.Modified;
}
else
{
dbCtx
var currData = localDbCtx
.DbSetPlantSupplWeekPlan
.Add(updItem);
.Where(x => x.WeekPlanId == updItem.WeekPlanId)
.FirstOrDefault();
if (currData != null)
{
currData.DayNum = updItem.DayNum;
currData.DeliveryHour = updItem.DeliveryHour;
currData.Note = updItem.Note;
currData.PlantId = updItem.PlantId;
currData.SupplierId = updItem.SupplierId;
currData.TransporterId = updItem.TransporterId;
localDbCtx.Entry(currData).State = EntityState.Modified;
}
else
{
localDbCtx
.DbSetPlantSupplWeekPlan
.Add(updItem);
}
localDbCtx.SaveChanges();
done = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante WeekPlanUpdate{Environment.NewLine}{exc}");
}
dbCtx.SaveChanges();
done = true;
}
catch (Exception exc)
{ }
return done;
}
-7
View File
@@ -474,7 +474,6 @@ namespace GWMS.UI.Data
{
done = dbController.OrderDelete(currItem);
invalidateAllCache();
dbController.ResetController();
}
catch (Exception exc)
{
@@ -551,7 +550,6 @@ namespace GWMS.UI.Data
{
done = dbController.OrderUpdate(currItem);
invalidateAllCache();
dbController.ResetController();
}
catch (Exception exc)
{
@@ -730,11 +728,6 @@ namespace GWMS.UI.Data
{ }
}
public void ResetController()
{
dbController.ResetController();
}
public void rollBackEdit(object item)
{
dbController.rollBackEntity(item);
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>1.0.2109.2912</Version>
<Version>1.0.2109.2914</Version>
<UserSecretsId>95c9f021-52d1-4390-a670-5810b7b777b0</UserSecretsId>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<RunAnalyzersDuringBuild>true</RunAnalyzersDuringBuild>
-2
View File
@@ -322,7 +322,6 @@ namespace GWMS.UI.Pages
protected override async Task OnInitializedAsync()
{
DataService.ResetController();
AppMService.ShowSearch = false;
AppMService.PageName = "Ordini";
AppMService.PageIcon = "fas fa-file-invoice pr-2";
@@ -382,7 +381,6 @@ namespace GWMS.UI.Pages
protected async Task UpdateData()
{
currRecord = null;
DataService.ResetController();
await ReloadData();
}
-2
View File
@@ -337,7 +337,6 @@ namespace GWMS.UI.Pages
protected override async Task OnInitializedAsync()
{
DataService.ResetController();
AppMService.ShowSearch = false;
AppMService.PageName = "Ordini";
AppMService.PageIcon = "fas fa-file-invoice pr-2";
@@ -396,7 +395,6 @@ namespace GWMS.UI.Pages
protected async Task UpdateData()
{
currRecord = null;
DataService.ResetController();
await DataService.PlantsAnalisysReset(AppMService.Order_Filter);
await ReloadData();
}
-2
View File
@@ -267,7 +267,6 @@ namespace GWMS.UI.Pages
protected override async Task OnInitializedAsync()
{
DataService.ResetController();
AppMService.ShowSearch = false;
AppMService.PageName = "Fornitore";
AppMService.PageIcon = "fas fa-industry pr-2";
@@ -325,7 +324,6 @@ namespace GWMS.UI.Pages
protected async Task UpdateData()
{
currRecord = null;
DataService.ResetController();
await ReloadData();
}
-2
View File
@@ -294,7 +294,6 @@ namespace GWMS.UI.Pages
protected override async Task OnInitializedAsync()
{
DataService.ResetController();
SelPlantId = 0;
SelTranspId = 0;
AppMService.ShowSearch = false;
@@ -373,7 +372,6 @@ namespace GWMS.UI.Pages
protected async Task UpdateData()
{
currRecord = null;
DataService.ResetController();
await ReloadData();
}
+1 -1
View File
@@ -1,7 +1,7 @@
{
"Logging": {
"LogLevel": {
"Default": "Trace",
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
+1 -1
View File
@@ -1,6 +1,6 @@
<body>
<i>GWMS - Gas Warehouse Management System</i>
<h4>Versione: 1.0.2109.2912</h4>
<h4>Versione: 1.0.2109.2914</h4>
<br /> Note di rilascio:
<ul>
<li>
+1 -1
View File
@@ -1 +1 @@
1.0.2109.2912
1.0.2109.2914
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<item>
<version>1.0.2109.2912</version>
<version>1.0.2109.2914</version>
<url>http://nexus.steamware.net/repository/SWS/GWMS/stable/0/GWMS.UI.zip</url>
<changelog>http://nexus.steamware.net/repository/SWS/GWMS/stable/0/ChangeLog.html</changelog>
<mandatory>false</mandatory>