问题描述
请参阅下面的code。如果我初始化一个以上的实体范围内,然后我得到的第二套code仅以下异常。如果我注释掉它的工作原理第二套。
请注意,这是一个示例应用程序,我知道这是没有意义的创造2上下文连胜。但是,生产code确实有理由在同一个的TransactionScope
创建多个环境,这不能改变。
修改
下面是我的previous问题,试图建立MS-DTC。似乎在服务器和客户端上被激活。我不知道它是否设置正确。还要注意的是,我想这样做的原因之一是,现有的的TransactionScope
使用ADO.NET和LINQ 2的Sql ......我会在code像那些使用相同的事务也。 (这可能听起来很疯狂,但我需要做如果可能的话它的工作)。
解决方案
Windows防火墙阻断连接到MS-DTC。的
使用(TransactionScope的TS =新System.Transactions.TransactionScope())
{
使用(DatabaseEntityModel O =新DatabaseEntityModel())
{
VAR V =(从S在o.Advertiser选择S)。首先();
v.AcceptableLength = 1;
o.SaveChanges();
}
// - >通过注释掉这一部分,它的工作原理
使用(DatabaseEntityModel O =新DatabaseEntityModel())
{
//异常在下面一行
VAR V =(从S1在o.Advertiser选择S1)。首先(); v.AcceptableLength = 1;
o.SaveChanges();
}
// - >
ts.Complete();
}
您MS-DTC(分布式事务协调器)无法正常工作的某些原因。 MS-DTC来协调跨多个异构资源,包括多个SQL连接交易的结果。
看看this链接对正在发生的事情的详细信息。
基本上,如果你确定你的MS-DTC正在运行,并且工作正常,你应该没有问题,使用2 ADO.NET连接 - 无论是实体框架连接或任何其他类型的
See the code below. If I initialize more than one entity context, then I get the following exception on the 2nd set of code only. If I comment out the second set it works.
Note that this is a sample app and I know it doesn't make sense to create 2 contexts in a row. However, the production code does have reason to create multiple contexts in the same TransactionScope
, and this cannot be changed.
Edit
Here is a previous question of me trying to set up MS-DTC. It seems to be enabled on both the server and the client. I'm not sure if it is set up correctly. Also note that one of the reasons I am trying to do this, is that existing code within the TransactionScope
uses ADO.NET and Linq 2 Sql... I would like those to use the same transaction also. (That probably sounds crazy, but I need to make it work if possible).
How do I use TransactionScope in C#?
Solution
Windows Firewall was blocking the connections to MS-DTC.
using(TransactionScope ts = new System.Transactions.TransactionScope())
{
using (DatabaseEntityModel o = new DatabaseEntityModel())
{
var v = (from s in o.Advertiser select s).First();
v.AcceptableLength = 1;
o.SaveChanges();
}
//-> By commenting out this section, it works
using (DatabaseEntityModel o = new DatabaseEntityModel())
{
//Exception on this next line
var v = (from s1 in o.Advertiser select s1).First(); v.AcceptableLength = 1;
o.SaveChanges();
}
//->
ts.Complete();
}
Your MS-DTC (Distributed transaction co-ordinator) is not working properly for some reason. MS-DTC is used to co-ordinate the results of transactions across multiple heterogeneous resources, including multiple sql connections.
Take a look at this link for more info on what is happening.
Basically if you make sure your MS-DTC is running and working properly you should have no problems with using 2 ADO.NET connections - whether they are entity framework connections or any other type.
这篇关于为什么不与实体框架的TransactionScope的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!