Is it possible to run several ExecuteUpdateAsync methods in 1 transaction?
For example, I have to bulk update several sets of data, but if later updates fail, want to rollback earlier updates
await _context.Groups
.Where(a => request.Groups.Contains(a.Guid))
.ExecuteUpdateAsync(s => s.SetProperty(u => u.ServiceId, u => request.ServiceId), cancellationToken);
await _context.Services
.Where(a => ...)
.ExecuteUpdateAsync(s => s.SetProperty(...), cancellationToken);
Sure, just open a transaction directly on the Database object
await using (var transaction = await _context.Database.BeginTransactionAsync(cancellationToken))
{
await _context
.Groups
.Where(a => request.Groups.Contains(a.Guid))
.ExecuteUpdateAsync(s => s.SetProperty(u => u.ServiceId, u => request.ServiceId),
cancellationToken);
await _context
.Services
.Where(a => ...)
.ExecuteUpdateAsync(s => s.SetProperty(...),
cancellationToken);
await transaction.CommitAsync(cancellationToken);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With