using (var transaction = context.Database.BeginTransaction()) { try { // perform database operations context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw; } }
using (var transaction = context.Database.BeginTransaction()) { // perform database operations context.SaveChanges(); transaction.Commit(); }In this example, a transaction is started using `BeginTransaction`, a single database operation is performed using `SaveChanges`, and then the transaction is committed to the database using `Commit`. Package library: Microsoft.EntityFrameworkCore Note: Both examples assume that `context` is an instance of a class that implements `DbContext`.