为什么TransactionScope不能与Entity Framework协同工作? [英] Why doesn't TransactionScope work with Entity Framework?

查看:321
本文介绍了为什么TransactionScope不能与Entity Framework协同工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅下面的代码。如果我初始化多个实体上下文,那么我在第二组代码之间得到以下异常。如果我注释掉第二组它可以工作。


{底层提供程序在Open上失败。}



Inner:{与底层事务管理器的通信失败。}



Inner:{Error HRESULT E_FAIL已从调用COM组件。}


请注意,这是一个示例应用程序,我知道创建2是没有意义的上下文连续。但是,生产代码确实有理由在同一个 TransactionScope 中创建多个上下文,并且无法更改。



修改



以下是我尝试设置MS-DTC的上一个问题。它似乎在服务器和客户端都启用。我不知道它是否正确设置。还要注意,我试图这样做的原因之一是, TransactionScope 中的现有代码使用ADO.NET和Linq 2 Sql ...我希望这些也使用相同的事务。 (这可能听起来很疯狂,但是如果可能,我需要使其工作)。



如何在C#中使用TransactionScope?



解决方案 / p>

Windows防火墙阻止与MS-DTC的连接。

 使用(TransactionScope ts = new System.Transactions.TransactionScope())
{
using(DatabaseEntityModel o = new DatabaseEntityModel())
{
var v =(来自o.Advertiser select s中的s).First();
v.AcceptableLength = 1;
o.SaveChanges();
}

// - >通过注释这个部分,它工作
使用(DatabaseEntityModel o = new DatabaseEntityModel())
{
//在下一行的异常
var v =(从s1在o广告选择s1).First(); v.AcceptableLength = 1;
o.SaveChanges();
}
// - >

ts.Complete();
}


解决方案

您的MS-DTC交易协调员)由于某种原因不能正常工作。 MS-DTC用于协调跨多个异构资源(包括多个sql连接)的事务结果。



查看此链接有关发生的更多信息。 / p>

基本上,如果您确保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.

{"The underlying provider failed on Open."}

Inner: {"Communication with the underlying transaction manager has failed."}

Inner: {"Error HRESULT E_FAIL has been returned from a call to a COM component."}

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不能与Entity Framework协同工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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