TransactionScope to delete associated data

47 views Asked by At

Im trying to use TransactionScope so that i can delete all associated data to the premiseId in order.

However, im getting error "The transaction specified for TransactionScope has a different IsolationLevel than the value requested for the scope. (Parameter 'transactionOptions')"

What's causing this?

 using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
        {
            try
            {
                List<HMOTask> getHMOTasks = (await _unitOfWork.HMOTaskRepository.GetNoLockAsync(e => e.PremisesID == Guid.Parse(premiseId))).ToList();
                if (getHMOTasks != null)
                {
                    foreach (HMOTask task in getHMOTasks)
                    {
                        List<HMOStep> getHMOSteps = (await _unitOfWork.HMOStepRepository.GetNoLockAsync(e => e.TaskID == task.TaskID)).ToList();                           
                        if (getHMOSteps != null)
                        {
                            foreach (HMOStep step in getHMOSteps)
                            {
                                await _unitOfWork.HMOStepRepository.DeleteAsync(step.StepID);                                    
                            }
                            await _unitOfWork.SaveAsync();                               
                        }
                        await _unitOfWork.HMOTaskRepository.DeleteAsync(task.TaskID);
                    }
                    await _unitOfWork.SaveAsync();
                }
                await _unitOfWork.HMOPremisesRepository.DeleteAsync(Guid.Parse(premiseId));

                await _unitOfWork.SaveAsync();

                transactionScope.Complete();

                return new ApiResponseBody();
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                transactionScope.Dispose();
            }
        }
0

There are 0 answers