如何在没有MSDTC的TransactionScope内运行两个实体框架上下文? [英] How to run two Entity Framework Contexts inside TransactionScope without MSDTC?

查看:188
本文介绍了如何在没有MSDTC的TransactionScope内运行两个实体框架上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题在这里的一个简单的例子中不容易重现,但是想知道是否有任何人有任何经验和技巧,这里是一个问题:

This problem is not readily reproducible in a simple example here but was wondering if anyone has any experience and tips, here is the issue:


    <使用实体框架
  • 在应用程序中有许多点,其中(1)数据写入一些实体表,例如客户,(2)数据写入历史

  • 这些操作都使用实体框架,但是,他们使用不同的上下文

  • 这些操作需要在一个事务中:即如果一个不能写,另一个不应该写等。

  • 我可以用TransactionScope包装它们,

  • using Entity Framework
  • have many points in application where (1) data is written to some entity table e.g. Customer, (2) data is written to history table
  • both of these actions use Entity Framework, HOWEVER, they use different contexts
  • these actions need to be both in one transaction: i.e. if one fails to write, the other should not write, etc.
  • I can wrap them with a TransactionScope,

这样:

using (TransactionScope txScope = new TransactionScope()) {
    ...
}

但这给了我:

Microsoft Distributed Transaction Coordinator (MSDTC) is disabled for network transactions.

我们的数据库管理员告诉我, MSDTC 被选择和无法安装

Our database admin has told me that MSDTC is disabled by choice and can not be installed.

因此,我正在进行更改,尝试使用MetadataWorkspace创建自己的EntityConnection,其想法是每个上下文将使用相同的EntityConnection 。然而,这证明几乎不可能试图让它工作,例如目前我仍然会得到上述错误,即使理论上两个上下文都使用EntityConnection。很难理解实体框架在哪里/为什么需要MSDTC。

Hence I am making changes trying to create my own EntityConnection with a MetadataWorkspace with the idea that each context will use the same EntityConnection. However, this is proving near impossible trying to get it to work, e.g. currently I continue to get the above error even though theoretically both contexts are using EntityConnection. It's difficult to understand where/why Entity Framework is requiring the MSDTC for example.

有没有人在这条路上走过,有经验或代码示例来分享?

推荐答案

嗯,问题很简单。

如果你使用sql server 2008,你不应该有这个问题,因为你有可执行的事务,而且.NET知道你正在使用相同的持久性存储(数据库),它不会将它推广到DTC,并将其作为本地提交。 使用sql server 2008查找可升级的事务。

If you are using sql server 2008 you should not have that problem because you have promotable transaction, and as .NET knows that you are using the same persistence store (the database) it wont promote it to DTC and commit it as local. look into promotable transaction with sql server 2008.

据我所知,Oracle正在其驱动程序中支持可升级的事务,但是我不知道该状态,MS oracle驱动程序不支持它。
http:// www.oracle.com/technology/tech/windows/odpnet/col/odp.net_11.1.0.7.20_twp.pdf

As far as I know Oracle is working in its driver to support promotable transactions, but I do not know the state, MS oracle driver does not support it. http://www.oracle.com/technology/tech/windows/odpnet/col/odp.net_11.1.0.7.20_twp.pdf

如果您正在使用不支持可升级事务的驱动程序,.NET不可能使用本地事务执行两个连接。您应该更改架构或说服数据库管理员安装MSDTC。

If you are using a driver that do not support promotable transactions it is impossible for .NET to use local transaction doing two connections. You should change your architecture or convince the database admin for installing MSDTC.

这篇关于如何在没有MSDTC的TransactionScope内运行两个实体框架上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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