C#C
C#2y ago
SWEETPONY

✅ transaction in transaction issue

I have this error:
The connection is already in a transaction and cannot participate in another transaction.

My code:
public override async Task<OperationResult> AddOrUpdateWorkingTasksAsync(
        IReadOnlyList<Infrastructure.Data.WorkingTask> workingTasks,
        EventHandlingContext context)
    {
        var transaction = await BeginTransactionAsync(context).ConfigureAwait(false);
        await using(transaction)
        {
            foreach(var workingTask in workingTasks)
            {
                var existTask = await FindAsync(workingTask.Id, context).ConfigureAwait(false);
                if(existTask != null)
                    await UpdateAsync(workingTask, context).ConfigureAwait(false);
                else
                    await AddAsync(workingTask, context).ConfigureAwait(false);
            }
            await eventDispatcher.WorkingTasksUpdatedAsync(workingTasks!, context).ConfigureAwait(false);
            await transaction.CommitAsync(context.CancellationToken).ConfigureAwait(false);
        }
        return OperationResult.Success;
    }

public async Task AddAsync(TEntity entity, TContext context)
    {
        var transaction = await rootRepository.BeginTransactionAsync(context).ConfigureAwait(false);
        await using(transaction)
        {
            await AddCoreAsync(entity, context).ConfigureAwait(false);
            await transaction.CommitAsync(context.CancellationToken).ConfigureAwait(false);
        }
    }


should
AddAsync
contain a transaction?
Was this page helpful?
✅ transaction in transaction issue - C#