diff --git a/entity-framework/core/saving/transactions.md b/entity-framework/core/saving/transactions.md index 0ba4ef2dd9..c29b948ca5 100644 --- a/entity-framework/core/saving/transactions.md +++ b/entity-framework/core/saving/transactions.md @@ -2,7 +2,7 @@ title: Transactions - EF Core description: Managing transactions for atomicity when saving data with Entity Framework Core author: SamMonoRT -ms.date: 9/26/2020 +ms.date: 08/19/2026 uid: core/saving/transactions --- # Using Transactions @@ -18,6 +18,25 @@ By default, if the database provider supports transactions, all changes in a sin For most applications, this default behavior is sufficient. You should only manually control transactions if your application requirements deem it necessary. +## Controlling automatic transactions + +You can control whether EF automatically creates a transaction when calling `SaveChanges` and no user transaction exists. Set to one of the following values: + +* (default): EF creates a transaction only when needed. For example, most single SQL statements execute in a transaction implicitly, so EF doesn't create an explicit transaction. +* : EF always creates a transaction if no user transaction exists. This may add database roundtrips, which can degrade performance. +* : EF never creates a transaction automatically. + +For example, configure EF to always create a transaction before calling `SaveChanges`: + +```csharp +context.Database.AutoTransactionBehavior = AutoTransactionBehavior.Always; +``` + +`Always` can be useful if application code relies on transaction creation callbacks being invoked when `SaveChanges` doesn't use a user transaction. + +> [!WARNING] +> Use `AutoTransactionBehavior.Never` with caution. If `SaveChanges` needs to execute multiple commands and a failure occurs, earlier commands may have already been committed, leaving partial changes in the database. + ## Controlling transactions You can use the `DbContext.Database` API to begin, commit, and rollback transactions. The following example shows two `SaveChanges` operations and a LINQ query being executed in a single transaction: