6

我刚刚针对 SQL 2010 Express 本地数据库,使用 EF 4.3 Code First 向 MVC3 应用程序介绍了 TransactionScope 的使用。当我在范围内尝试 SaveChanges 时,我收到“Provider failed to open”通知,内部异常是关于缺少 MSDTC。据我所知,只有当我使用多个连接字符串时才会发生这种情况。我只使用到一个数据库的连接,我在应用程序中只有 1 个字符串。但是,我确实使用了几个 DbContext 实例,但在事务范围内只使用了一个。

我能做些什么来解决这个问题?

4

3 回答 3

4

不知道是不是和这个帖子一样? 如何在没有 MSDTC 的情况下在 TransactionScope 中运行两个实体框架上下文?

解决方法是:

0) 创建可推广事务(取决于 SQL 服务器版本?)(http://msdn.microsoft.com/en-us/library/ms172070.aspx

1)实体框架 - MSDTC Gotchya(http://joeknowsdotnet.wordpress.com/2012/07/19/entity-framework-msdtc-gotchya/

2)避免不必要的分布式事务升级(http://petermeinl.wordpress.com/2011/03/13/avoiding-unwanted-escalation-to-distributed-transactions/

于 2013-09-27T03:30:01.860 回答
1

The first time your application makes a call to EF it will do the initialization (it will build the db if it doesn't exist etc.) If this first call is inside a TransactionScope it will promote to DTC.

To fix this, make a call to EF in your app startup method and this will ensure that EF will be initialized outside of the TransactionScope. From here on in you can include one or more calls to EF inside a TransactionScope and it won't promote to DTC providing: a) You always use EXACTLY the same connection string, and b) You are using Sql Server 2008 and above (which you are).

于 2013-03-01T13:06:36.927 回答
0

我认为,如果您有一个事务范围并打开两个单独的连接,即使它们使用相同的连接字符串连接到相同的数据库,您也会发现该事务将被提升为分布式事务。

http://www.bomisofmab.com/blog/?p=184充分描述了这种情况。

于 2012-07-12T11:52:03.397 回答