在EF Core 2.1中使用环境事务时,是否需要手动关闭DbConnection? [英] Do I need to close the DbConnection manually when using ambient transactions with EF Core 2.1?

查看:95
本文介绍了在EF Core 2.1中使用环境事务时,是否需要手动关闭DbConnection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EF Core 2.1引入了对环境事务的支持.示例创建新的SqlConnection ,手动将其打开并将其传递给 DbContext :

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
    }
}

虽然没有调用 connection.Close().

样本中是否只是缺少此部分?或者在处置 TransactionScope DbContext 后,连接是否以某种方式自动关闭了?

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

关闭/处置的调用丢失了.我提出了提取请求,并且文档现在已更新.

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

推荐答案

该行为似乎与环境交易无关,但问题谁拥有 DbConnection 的答案被传递给了 DbContext .

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 构造函数具有用于明确指定该参数的 bool contextOwnsConnection 参数.

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

但是EF Core呢?接受 DbConnection UseXyz 方法上没有此类参数.

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:

如果连接处于打开状态,则EF将不会打开或关闭连接.如果连接处于关闭状态,则EF将根据需要打开和关闭连接.

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.

我读到如果未打开通过的连接,EF Core将获得所有权,否则所有权将留给调用方".

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

由于该示例在 UseSqlServer(connection)之前调用 connection.Open(); ,因此我假设您负责关闭/处置它,因此我认为示例不正确.

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.

这篇关于在EF Core 2.1中使用环境事务时,是否需要手动关闭DbConnection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆