I am trying to use TransactionScope
to make my db context operations spread across different files run under one transaction, so all or none save should happen.
The problem is it is giving me this error even before the saving of entities start. It gives me the error when I just try to fetch data using the db context
Cannot enlist in the transaction because a local transaction is in progress on the connection. Finish local transaction and retry.
This is my code
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
// this first line itself is giving me the error even when I am just trying to select data here and
// have not even started saving anything to the database
IEnumerable<FormHierarchy> formHierarchies = await _unitOfWork.FormHierarchyRepository.GetAllowedFormHierarchiesForADeclarationTypeAsync(model.DeclarationType);
// A lot of code for saving the entities follows this
}
This is the repository method for selecting data
public class FormHierarchyRepository : Repository<FormHierarchy>, IFormHierarchyRepository
{
private readonly IDapperQueryManager _queryManager;
public readonly PJDbContext _dbContext;
public FormHierarchyRepository(IDapperQueryManager queryManager, PJDbContext dbContext) : base(dbContext)
{
_dbContext = dbContext;
_queryManager = queryManager;
}
public async Task<IEnumerable<FormHierarchy>> GetAllowedFormHierarchiesForADeclarationTypeAsync(DeclarationTypes declarationType)
{
return await _dbContext.FormHierarchy.Where(x => x.DeclarationTypeId == (int)declarationType).ToListAsync();
}
}
If this error would have come when I would have started saving the entities I would have understood it and would have tried to adjust some code following these type of posts
But it is coming on the data selection which ideally should not be even using the transaction in that sense.
Any idea what could be going wrong and what should I try?