最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Cannot enlist in the transaction because a local transaction is in progress on the connection. Finish local transaction and

programmeradmin1浏览0评论

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?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论