Praveen Kumar

Praveen Kumar

  • NA
  • 235
  • 22.5k

You cannot call savechanges() more than once on a dbcontextscope.

Jul 13 2020 11:10 PM
HI,
I am getting this error when trying to run multiple failed billing.Can any one help me to fix this.
 
 
Controller
  1. [HttpPost]  
  2. [ValidateAntiForgeryToken]  
  3. public async Task<ActionResult> ReRunFailedSelectedBilling(List<BillingFailedViewModel> FailedGroups)  
  4. {  
  5. if (_logger.IsInfoEnabled)  
  6. _logger.LogInfo(string.Format("Entering ReRunFailedSelectedBilling"));  
  7. List<BillingFailedViewModel> CGs = FailedGroups.Where(x => x.ReRunFlag).ToList();  
  8. // Re-using code to get something working for GPSI. This should actually remove all the CGs and then process a single WFR. For now, we'll process one WFR for each CG...  
  9. using (var dbContextScope = _dbContextScopeFactory.Create())  
  10. {  
  11. foreach (BillingFailedViewModel group in CGs)  
  12. {  
  13. ReRunModel run = new ReRunModel((int)group.Id, DbConnection, ServerURL);  
  14. var login = HttpContext.User.Identity.Name;  
  15. run.Reject(login);  
  16. if (run._Status.Success == false)  
  17. {  
  18. TempData["Failure"] = "Error in rejection. Failed to reject.";  
  19. }  
  20. else  
  21. {  
  22. // Create a new workflow request and re-run  
  23. var wfr = run.SetWorkflowRequest();  
  24. dbContextScope.SaveChanges();  
  25. run.WorkflowRequestId = wfr.Id;  
  26. await run.RunAsync(login);  
  27. }  
  28. }  
  29. }  
  30. using (var dbContextScope = _dbContextScopeFactory.Create())  
  31. {  
  32. if (_logger.IsInfoEnabled)  
  33. _logger.LogInfo("Exiting ReRunFailedSelectedBilling");  
  34. var query = FailedGroups.First().Query;  
  35. return RedirectToAction("Billing""Manage"new { Area = "Billing", CalendarPeriodId = query.CalendarPeriodId, WorkflowGroupId = query.WorkflowGroupId, CompanyId = query.CompanyId, GroupSearch = query.GroupSearch, DisplayBucket = query.DisplayBucket, ShowWorkflowRequests = true });//, calendarPeriodId = FailedGroups.First().QueryPeriodId });  
  36. }  
  37. }  
Model.cs
  1. public async Task RunAsync(string login)  
  2. {  
  3. logger.LogInfo(string.Format("PERF RunAsync starting"));  
  4. // Kick off the server job  
  5. string customer = _WfRepo.GetCurrentUserCustomer(_User.WindowsUserName).Customer.Code;  
  6. ServiceStatus status = new ServiceStatus { Success = false, Message = "Run failed." };  
  7. if (WorkflowRequestId!=null)  
  8. {  
  9. BusinessActionService.WorkflowRequest request = new BusinessActionService.WorkflowRequest()  
  10. {  
  11. WorkflowRequestId = WorkflowRequestId.ToString(),  
  12. CustomerCode = customer,  
  13. UserName = _User.WindowsUserName,  
  14. LoginName = login  
  15. };  
  16. try  
  17. {  
  18. logger.LogInfo(string.Format("PERF RequestBillingAsync {0} called", WorkflowRequestId.ToString()));  
  19. await _Service.RequestBillingAsync(request).ConfigureAwait(false);  
  20. }  
  21. catch (Exception e)  
  22. {  
  23. logger.LogError("RequestBilling failed ", e);  
  24. }  
  25. logger.LogInfo(string.Format("PERF RequestBillingAsync {0} returned", WorkflowRequestId.ToString()));  
  26. }  
  27. logger.LogInfo(string.Format("PERF RunAsync ending "));  
  28. }  

Answers (5)