Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mikerains-valorem opened this issue Apr 11, 2024 · 0 comments

Comments

@mikerains-valorem
Copy link

mikerains-valorem commented Apr 11, 2024

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<BloggingContext>()
            .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<BloggingContext>()
            .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

@ajcvickers ajcvickers added this to the Backlog milestone May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants