ObjectContext.Connection.BeginTransaction()是否使用MSDTC? [英] Does ObjectContext.Connection.BeginTransaction() use MSDTC?

查看:231
本文介绍了ObjectContext.Connection.BeginTransaction()是否使用MSDTC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确认实体框架的ObjectContext.Connection.BeginTransaction()方法返回的事务是否使用MSDTC(Microsoft Distributed Transaction Coordinator)的支持?

I want confirm if the Transaction that Entity Framework's ObjectContext.Connection.BeginTransaction() method returns uses the support of MSDTC (Microsoft Distributed Transaction Coordinator) or not?

有没有办法在不支持MSDTC的情况下使用交易?

Is there any way to use transactions without support of MSDTC?

推荐答案

在某些情况下,它会自动促成由MSDTC协调的交易。每次调用 ObjectContext.SaveChanges()时,如果还没有一个新的事务被创建(如果范围内已经有一个活动的事务,它将被激活在该交易中)。但是,默认情况下,每次调用 ObjectContext.SaveChanges()时,连接也将被打开和关闭。所以如果在方法的开头调用 ObjectContext.Connection.BeginTransaction(),那么调用 ObjectContext.SaveChanges()多次持有原始事务,有一些版本的SQL Server和实体框架,这可能导致事务升级到MSDTC,因为它现在在单个事务中使用不同的连接。如果您尝试避免事务升级到MSDTC,那么请在开始时明确地打开您的连接,并在完成后将其关闭:

It will automatically promote to a transaction coordinated by MSDTC under certain conditions. Each time you call ObjectContext.SaveChanges() a new transaction is created if there isn't already one in scope (if there is already an active transaction in scope, it will enlist in that transaction). However, by default, the connection will also be opened and closed each time you call ObjectContext.SaveChanges(). So if you're calling ObjectContext.Connection.BeginTransaction() at the beginning of a method, then calling ObjectContext.SaveChanges() multiple times while holding onto the original transaction, with some versions of SQL Server and Entity Framework, this can cause the transaction to get promoted to MSDTC because it's now using different connections within a single transaction. If you're trying to avoid your transaction getting promoted to MSDTC, then explicitly open your connection at the beginning and close it when you're done:

using(MyEntities context = new MyEntities())
using(DbConnection conn = context.Connection)
{
    conn.Open();
    DbTransaction transaction = conn.BeginTransaction();

    // now do stuff within the transaction scope

    transaction.Commit();
}

但是,建议您使用TransactionScope,因为它更灵活,更少平台依赖,如果将来您决定实际需要MSDTC,那将使您更容易。如果有活动的TransactionScope,EntityFramework将自动加入事务:

However, it's recommended that you use a TransactionScope, as it's more flexible, less platform-dependent, and will make it easier on you if in the future you decide you do actually need to something that requires MSDTC. EntityFramework will automatically enlist in the transaction if there's an active TransactionScope:

using(TransactionScope transaction = new TransactionScope())
using(MyEntities context = new MyEntities())
using(DbConnection conn = context.Connection)
{
    conn.Open();

    // now do stuff within the transaction scope

    transaction.Complete();
}

这篇关于ObjectContext.Connection.BeginTransaction()是否使用MSDTC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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