I am trying to implement roll back the transaction in asp net boilerplate, so if the delete of the subsequent entity throws an exemption I need to roll back the delete of the previous entity, with the following code, the first delete is committed and is not rolled back on catch of exemption, and help over why is much appreciated.
the controller
public ActionResult Expense_Destroy([DataSourceRequest] DataSourceRequest request, TS_Expense_ViewModel expenseVM)
{
if (expenseVM != null)
{
try
{
//Delete The Expense and Expense Audit.....
_iTS_Emp_Expense_AppService.Delete(expenseVM.TS_Expense_VMID);
}
catch (Exception ex)
{
string exmptionText = _exemption_AppService.GetTimeTenantUserStamp + ex.Message;
ModelState.AddModelError(string.Empty, exmptionText);
Logger.Error(exmptionText + ex.StackTrace);
}
}
return Json(new[] { expenseVM }.ToDataSourceResult(request, ModelState));
}
then the application services
public void Delete(int id)
{
//Delete Expense.
_iTS_Emp_Expense_Manager.Delete(id);
//Delete Expense Audit.
_iTS_Audit_Expense_Manager.DeleteByExpenseId(id);
}
and the entity management survives
public void DeleteByExpenseId(int id) //Expense Id
{
var expense = _repositoryAuditExpense.FirstOrDefault(p => p.ExpenseID == /*id*/88);
if (expense == null)
{
throw new Exception(CustomMessageConsts.ServerGetError);
}
_repositoryAuditExpense.Delete(expense);
}
what happens, if DeleteByExpenseId throws an exemptions, the _iTS_Emp_Expense_Manager.Delete(id); still commits the delete of the previous entity, I tried to manage the unit of work scope inside the application services by wrapping the two interfaces inside a using statement but this also did not work. the code I tried looks as follows:
public void Delete(int id)
{
using (var unitOfWork = _unitOfWorkManager.Begin())
{
//Delete Expense.
_iTS_Emp_Expense_Manager.Delete(id);
//Delete Expense Audit.
_iTS_Audit_Expense_Manager.DeleteByExpenseId(id);
unitOfWork.Complete();
}
}