C
C#9mo 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 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);
}
}
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?
3 Replies
Jimmacle
Jimmacle9mo ago
what ORM is this?
SWEETPONY
SWEETPONYOP9mo ago
ef core
Jimmacle
Jimmacle9mo ago
not sure why you closed, but when you SaveChanges in EF core that all runs in a transaction already
Want results from more Discord servers?
Add your server