实体框架 4 中两个不同实体的事务
我有不同数据库的两个不同实体。 在我的过程中,我想操作两个数据库,并且整个过程必须处于事务状态。 我尝试过使用 TransactionScope。它适用于单个实体。
var entity1 = clsProject.GetEntity1();
var entity2 = clsProject.GetEntity2();
System.Transactions.TransactionScope tranScope = new System.Transactions.TransactionScope();
entity1.Connection.Open();
entity2.Connection.Open();
//Do operations
entity1.SaveChanges(false);
entity2.SaveChanges(false);
tranScope.Complete();
entity1.AcceptAllChanges();
entity2.AcceptAllChanges();
但是,它在这一行引发错误:entity2.Connection.Open(); 错误消息:底层提供程序在打开时失败。 内部异常:合作伙伴事务管理器已禁用其对远程/网络事务的支持。 (HRESULT 异常:0x8004D025)
请给出您的想法。
提前致谢,
纳雷什·戈拉达拉
I have two different entities of different databases.
In my process I want to manipulate both databases, and the whole process must be in transaction.
I have tried with TransactionScope. It works fine for single entity.
var entity1 = clsProject.GetEntity1();
var entity2 = clsProject.GetEntity2();
System.Transactions.TransactionScope tranScope = new System.Transactions.TransactionScope();
entity1.Connection.Open();
entity2.Connection.Open();
//Do operations
entity1.SaveChanges(false);
entity2.SaveChanges(false);
tranScope.Complete();
entity1.AcceptAllChanges();
entity2.AcceptAllChanges();
But, it raises an error at this line:entity2.Connection.Open();
Error Message: The underlying provider failed on Open.
Inner Exception: The partner transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0x8004D025)
Please give your ideas for this.
Thanks in Advance,
Naresh Goradara
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的第二个数据库位于另一台服务器上,则需要打开
If your second database is on another server you need to turn on Distributed Transaction Coordinator on both servers, and TransactionScope will work.
如前所述,您需要启用分布式事务协调器,但您还需要将其配置为允许传入和/或传出连接。
另外:出于测试目的,您可以将其设置为不需要任何身份验证。
所有这些设置都可以在“组件服务”MMC 中“分布式事务协调器”的“安全”选项卡上找到。
所有这些都可以在“Eugene S.”提供的链接上找到:分布式事务协调器
另一件值得注意的事情是:微软有一个 DTC 测试工具,称为 DTCPing。您可以将此工具提取到要参与交易的两台服务器,同时在每台服务器上运行程序,并以“交易伙伴”名称(即另一台计算机的名称)命名。点击每台服务器上的运行按钮,该工具将为您运行一些测试 - 它也有一些相当好的错误报告。
As stated earlier, you'll need to enable the Distributed Transaction Coordinator, but you'll also need to configure it to allow incoming and/or outgoing connections.
In addition: for testing purposes, you can set it to not require any authentication.
All of these settings are available on the "Security" tab of the "Distributed Transaction Coordinator" in the "Component Services" MMC.
All of this can be found on the link provided by "Eugene S.": Distributed Transaction Coordinator
One other thing of note: Microsoft has a DTC testing tool known as DTCPing. You can extract this tool to both servers that are to be involved in the transaction, run the programs on each server simultaneously and ether the "transaction partners" name (which is just the name of the other computer). Hit the run button on each server and the tool will run some tests for you - it has some fairly good error reporting too.