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

