Update metodi x insert async in transazione
This commit is contained in:
@@ -372,6 +372,60 @@ namespace MP.Data.Controllers
|
||||
return await dbCtx.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Aggiunta record MicroStato + EventList
|
||||
/// </summary>
|
||||
/// <param name="newRecMsm"></param>
|
||||
/// <param name="newRecEv"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> EvListMicroStatoInsertAsync(MicroStatoMacchinaModel newRecMsm, EventListModel newRecEv)
|
||||
{
|
||||
// eseguo in transazione...
|
||||
await using var dbCtx = new MoonProContext(_configuration);
|
||||
await using var tx = await dbCtx.Database.BeginTransactionAsync();
|
||||
|
||||
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);
|
||||
#if false
|
||||
dbCtx.Entry(actRec).State = EntityState.Modified;
|
||||
#endif
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chiamata x stored recupero FluxLog x macchina (first)
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user