ADO实体框架是否支持非DTC交易?一个EntityContext和一个TransactionScope内的多个查询导致DTC升级 [英] Does ADO Entity Framework support non DTC transactions? Multiple queries inside one EntityContext and one TransactionScope is causing DTC promotion

查看:134
本文介绍了ADO实体框架是否支持非DTC交易?一个EntityContext和一个TransactionScope内的多个查询导致DTC升级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用实体框架的Web应用程序 - 我们利用TransactionScope类来提供环境事务。

I have a web application that uses the Entity Framework - we make use of the TransactionScope class to provide ambient transactions.

有没有办法告诉EF使用一个标准的T-SQL交易优于DTC交易?我们经常对一个EntityContext和一个TransactionScope实例中的不同表进行一些查询,但是这似乎总是将交易提升为DTC

Is there any way to tell EF to use a standard T-SQL transaction in preference to DTC transaction? Quite often we make a number of queries to different tables inside one EntityContext and one TransactionScope instance, however this seems to always promote the transaction to DTC

我已经举了一个简短的例子在一起,见下文。对单个表的查询正确启动T-SQL事务并且处于连接状态SPID 54
然后对ContactUs表进行查询,并且EF在具有敲击效果的不同连接(SPID 53)上执行此操作促销交易到DTC交易**

I have put a short example together, see below. The query to the individual table correctly starts a T-SQL transaction and was on connection SPID 54 Then the query to the ContactUs table is made and EF does this on a different connection (SPID 53) which has the knock on effect of promoting the transaction to a DTC transaction.**

using (MyEntities DB = new MyEntities())
{
    using (TransactionScope t = new TransactionScope())
    {
        DB.Individual.First().EmailAddress = "bob" + DateTime.Now.Second.ToString() + "@bob.com"; // done on connection 54
        DB.ContactUs.First(); // done on connection 53 thus promoting the transaction!!
        DB.SaveChanges();
        t.Complete();
    }
}


推荐答案


  1. 在连接字符串上设置 Enlist = false ,以避免自动登记事务。

  1. Set Enlist=false on connection string to avoid auto enlistment on transaction.

手动登录连接作为参与者的交易范围。 (http://msdn.microsoft.com/ en-us / library / ms172153%28v = VS.80%29.aspx)

Manually enlist connection as participants in transaction scope. (http://msdn.microsoft.com/en-us/library/ms172153%28v=VS.80%29.aspx)

这篇关于ADO实体框架是否支持非DTC交易?一个EntityContext和一个TransactionScope内的多个查询导致DTC升级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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