liveBook · Manning

Transaction

As explained in Building Web APIs with ASP.NET Core

A transaction is a database operation or sequence of operations treated as a single unit of work with defined consistency and completion behavior.

A transaction groups several database commands into one unit of work. Either the complete sequence succeeds, or the database avoids a partially applied change. section 5.3.6

During board-game seeding, one transaction turns IDENTITY_INSERT on, saves entities with their existing IDs, and turns the setting off again. section 5.3.6

A practical transaction sequence is:

  • Begin a transaction through the EF Core database context. section 5.3.6
  • Execute the SQL Server-specific IDENTITY_INSERT command. section 5.3.6
  • Save the entities, reset IDENTITY_INSERT, and commit the transaction. section 5.3.6

Seed endpoint

After the transaction is added, PUT /Seed returns HTTP 200 and reports 20,327 board games, 8 domains, 182 mechanics, and 16 skipped rows. section 5.3.6

// SAVE
using var transaction = _context.Database.BeginTransaction();
_context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT BoardGames ON");
await _context.SaveChangesAsync();
_context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT BoardGames OFF");
transaction.Commit();

Shows the compact implementation that wraps SQL commands and entity saving in one transaction. section 5.3.6

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains transaction boundaries through sequential EF Core commands but does not develop broader transaction theory.
Examples
Uses the PUT /Seed endpoint and an IDENTITY_INSERT save sequence as concrete transaction examples.
What you can do after
Apply via Entity Framework Core to make a multi-command seed operation execute atomically.
How it connects
Connects atomicity with EF Core, SQL Server, raw SQL, database seeding, and identity columns.

Also explained in

Compare how 2 books explain transaction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.