Your First Transaction
What this page covers#
This page shows how to start a transaction and run work inside it.
The simple version#
You begin a transaction from MooDbContext, then work through the returned MooTransaction.
await using var transaction = await db.BeginTransactionAsync(); await transaction.ExecuteAsync( "dbo.usp_User_UpdateDisplayName", new MooParams() .AddInt("@UserId", 1) .AddNVarChar("@DisplayName", "Ada Lovelace", 200)); await transaction.CommitAsync();
What is important here#
BeginTransactionAsync()starts the SQL Server transaction- all work done through
transactionshares the same connection and transaction - changes are committed only when you call
CommitAsync() - if the transaction is disposed without being committed, MooDb rolls it back automatically
- once committed, that transaction instance is complete and should not be reused for more commands
Isolation level#
You can choose an isolation level:
using System.Data; await using var transaction = await db.BeginTransactionAsync(IsolationLevel.Serializable);
Raw SQL and bulk inside a transaction#
A MooTransaction also exposes:
transaction.Sqltransaction.Bulk
So the transaction entry point keeps the same mental model as MooDbContext.