fun with transactions

This commit is contained in:
2020-05-10 22:58:48 +00:00
parent bd773e85b4
commit f3f9b0ace5

View File

@@ -199,14 +199,10 @@ namespace AyaNova.Biz
//collect the child id's to delete //collect the child id's to delete
var ItemIds = await ct.WorkOrderItem.Where(m => m.WorkOrderId == id).Select(m => m.Id).ToListAsync(); var ItemIds = await ct.WorkOrderItem.Where(m => m.WorkOrderId == id).Select(m => m.Id).ToListAsync();
//Delete children //Delete children
foreach (long ItemId in ItemIds) foreach (long ItemId in ItemIds)
{
if (!await ItemDeleteAsync(ItemId, transaction)) if (!await ItemDeleteAsync(ItemId, transaction))
return false; return false;
}
ct.WorkOrder.Remove(dbObject); ct.WorkOrder.Remove(dbObject);
await ct.SaveChangesAsync(); await ct.SaveChangesAsync();
@@ -422,8 +418,9 @@ namespace AyaNova.Biz
// //
internal async Task<bool> ItemDeleteAsync(long id, Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction parentTransaction = null) internal async Task<bool> ItemDeleteAsync(long id, Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction parentTransaction = null)
{ {
using (var transaction = parentTransaction ?? await ct.Database.BeginTransactionAsync()) Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction transaction = null;
{ if (parentTransaction == null)
transaction = await ct.Database.BeginTransactionAsync();
try try
{ {
WorkOrderItem dbObject = await ct.WorkOrderItem.SingleOrDefaultAsync(m => m.Id == id); WorkOrderItem dbObject = await ct.WorkOrderItem.SingleOrDefaultAsync(m => m.Id == id);
@@ -463,11 +460,14 @@ namespace AyaNova.Biz
{ {
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here //Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
throw; throw;
}
finally
{
if (parentTransaction == null)
await transaction.DisposeAsync();
} }
return true; return true;
} }
}
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////