dotnet / dotnet/EntityFramework.Docs

Doc does not address the most common use-case, when DbContext is injected

Open
#4,710 0 comments 0 reactions 0 assignees View on GitHub
area-transactions
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

### Type of issue

Missing information

### Description

Where the documentation describes using System.Transactions for Ambient transaction support, it only discusses when DbContext is constructed inside the TransactiopnScope using block. But this rarely happens in the real world.

In the real world, DbContext is injected into the class, so it and its SqlConnection are created before the using TransactionScope. In this case, it is unclear if the DbContext's SqlConnection will be enlisted in the TransactionScope or CommittableTransaction.

The Documentation has 2 examples.

First documentation Example:
````
using (var scope = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
{
using var connection = new SqlConnection(connectionString);
connection.Open();

try
{
// Run raw ADO.NET command in the transaction
var command = connection.CreateCommand();
command.CommandText = "DELETE FROM dbo.Blogs";
command.ExecuteNonQuery();

// Run an EF Core command in the transaction
var options = new DbContextOptionsBuilder()
.UseSqlServer(connection)
.Options;
using (var context = new BloggingContext(options))
{
context.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/dotnet" });
context.SaveChanges();
}
````
Question: Here BloggingContext is created form a connection that was created and opened INSIDE A TransactionScope. _**Will a DbContext injected into the class participate in the TransactionScope, since it is created with a SqlConnection BEFORE the method which creates the TransactionScope executes?**_

Second documentation example:
````
using (var transaction = new CommittableTransaction(
new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
{
var connection = new SqlConnection(connectionString);

try
{
var options = new DbContextOptionsBuilder()
.UseSqlServer(connection)
.Options;

using (var context = new BloggingContext(options))
{
context.Database.OpenConnection();
context.Database.EnlistTransaction(transaction);
````

Question: When DbContext is injected, can the client code call context.Database.EnlistTransaction(transaction) and expect the DbContext.SaveChanges() to participate in the CommittableTransaction?

### Page URL

https://learn.microsoft.com/en-us/ef/core/saving/transactions

### Content source URL

https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/saving/transactions.md

### Document Version Independent Id

cf56babd-def0-d76e-8eee-e33341b6a009

### Article author

@roji

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.