Do I need to close the DbConnection manually when using ambient transactions with EF Core 2.1?

我是研究僧i 提交于 2021-01-27 13:38:51

问题


EF Core 2.1 introduced support for ambient transactions. The sample creates a new SqlConnection, manually opens it and passes it to the DbContext:

using (var scope = new TransactionScope(
    TransactionScopeOption.Required, 
    new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
{
    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();
        }

        // Commit transaction if all commands succeed, transaction will auto-rollback
        // when disposed if either commands fails
        scope.Complete();
    }
    catch (System.Exception)
    {
        // TODO: Handle failure
    }
}

There is no call to connection.Close() though.

Is this part just missing in the sample or is the connection closed automatically somehow when the TransactionScope or the DbContext are disposed?

Edit: The call to Close/Dispose was missing. I filed a pull request and the docs are updated now.


回答1:


The behavior seems to be unrelated to ambient transactions, but the answer of the question who owns the DbConnection passed to a DbContext.

EF6 DbContext constructor accepting DbConnection has bool contextOwnsConnection parameter for explicitly specifying that.

But how about EF Core? There is no such parameter on UseXyz methods accepting DbConnection.

The rule seems to be the as follows, taken from UseSqlServer method connection parameter documentation:

If the connection is in the open state then EF will not open or close the connection. If the connection is in the closed state then EF will open and close the connection as needed.

Which I read "If the passed connection is not opened, EF Core will take the ownership, otherwise the ownership is left to the caller".

Since the example calls connection.Open(); before UseSqlServer(connection), I would assume you are responsible for closing/disposing it, so I would consider the example being incorrect.




回答2:


For safeside use using so that the connection is disposed.

using(var connection = new SqlConnection(connectionString)) {
   ....
}


来源:https://stackoverflow.com/questions/51239326/do-i-need-to-close-the-dbconnection-manually-when-using-ambient-transactions-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!