是否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
问题描述
我有一个使用实体框架的Web应用程序 - 我们利用TransactionScope类,提供环境交易
。有没有办法告诉EF要使用preference一个标准的T-SQL事务DTC事务?很多时候,我们做了一些查询内的一个的EntityContext和一个TransactionScope的实例不同表的,然而,这似乎永远促进交易DTC
我已经把一个小例子起来,见下文.... 查询到的各个表可以正常启动一个T-SQL交易,是在连接SPID 54 随后查询到联系我们表由和EF做到这一点在不同的连接(SPID 53),这对促进交易DTC事务中作用的敲门声。
使用(MyEntities DB =新MyEntities())
{
使用(TransactionScope的T =新的TransactionScope())
{
。DB.Individual.First()EmailAddress的=鲍勃+ DateTime.Now.Second.ToString()+@ bob.com; //完成对连接54
DB.ContactUs.First(); //完成对连接53从而推动交易!
DB.SaveChanges();
t.Complete();
}
}
-
将招募=假在连接字符串,以避免自动入伍的交易。
-
手动连接招募参与者在交易范围内。 <一href="http://msdn.microsoft.com/en-us/library/ms172153%28v=VS.80%29.aspx">(http://msdn.microsoft.com/en-us/library/ms172153%28v=VS.80%29.aspx)
I have a web application that uses the Entity Framework - we make use of the TransactionScope class to provide ambient transactions.
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
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();
}
}
Set Enlist=false on connection string to avoid auto enlistment on transaction.
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屋!