Docs / Run Work in a Transaction

Run Work in a Transaction

What this page covers#

This page shows the normal pattern for running several operations inside one MooTransaction.

Example#

</> C#
await using var transaction = await db.BeginTransactionAsync();

try
{
    await transaction.ExecuteAsync(
        "dbo.usp_User_UpdateDisplayName",
        new MooParams()
            .AddInt("@UserId", 1)
            .AddNVarChar("@DisplayName", "Ada Lovelace", 200));

    await transaction.CommitAsync();
}
catch
{
    throw;
}

Important notes#

  • if CommitAsync() is never called, MooDb rolls the transaction back when it is disposed
  • once committed, that transaction instance is complete and should not be reused
  • transaction.Sql and transaction.Bulk let you keep using the same mental model inside the transaction