Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion entity-framework/core/saving/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Starting with EF Core 7.0, you can control whether EF automatically creates a transaction when calling `SaveChanges` and no user transaction exists. Set <xref:Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.AutoTransactionBehavior> to one of the following values:
Comment thread
AndriySvyryd marked this conversation as resolved.
Outdated

* <xref:Microsoft.EntityFrameworkCore.AutoTransactionBehavior.WhenNeeded> (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.
* <xref:Microsoft.EntityFrameworkCore.AutoTransactionBehavior.Always>: EF always creates a transaction if no user transaction exists. This may add database roundtrips, which can degrade performance.
* <xref:Microsoft.EntityFrameworkCore.AutoTransactionBehavior.Never>: 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 <xref:Microsoft.EntityFrameworkCore.Diagnostics.IDbTransactionInterceptor> 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:
Expand Down
Loading