-
Notifications
You must be signed in to change notification settings - Fork 19
Nested (virtual) Transactions
Pawel Gerr edited this page Nov 11, 2025
·
1 revision
Enables calling BeginTransaction multiple times. On first call a database transaction is started, this transaction is considered to be a root translation. All further calls to BeginTransation are creating virtual child transactions. If and only if all transactions are completed by calling the method Commit then the underlying database transaction is going to be committed as well. In all other cases the database transaction is going to be rolled back if the root transaction is completed (i.e. committed/rolled back/disposed).
If BeginTransaction is called with an IsolationLevel to create a child transaction then the provided level is checked for compatibility with the level of the root transaction.
services
.AddDbContext<DemoDbContext>(builder => builder
//.UseSqlite("...")
.UseSqlServer("...")
.AddNestedTransactionSupport()// starts a database transaction
using var rootTx = myDbContext.Database.BeginTransaction();
...
// creates a virtual child transaction
using var childTx = myDbContext.Database.BeginTransaction();
// case 1: both transactions are committed
childTx.Commit();
rootTx.Commit(); // commits the database transaction
// case 2: outer transaction is rolled back
childTx.Commit();
rootTx.Rollback(); // the database transaction is rolled back
// case 3: both transactions are rolled back
childTx.Rollback();
rootTx.Rollback(); // the database transaction is rolled back
// case 4: inner transactions is rolled back
childTx.Rollback();
rootTx.Commit(); // throws TransactionAbortedException, later, e.g. on Dispose the database transaction is rolled back- Collection Parameters (temp-tables light) (SQL Server)
- Window Functions Support (RowNumber, Sum, Average, Min, Max)
- Nested (virtual) Transactions
- Table Hints (SQL Server)
- Queries accross multiple databases (SQL Server)
- Changing default schema at runtime
- If-Exists / If-Not-Exists checks in migrations (SQL Server)
- Migrations: include-columns (SQL Server)
- Migrations: identity column (SQL Server)
- Migrations: (non-)clustered PK (SQL Server)