Why is my TransactionScope trying to use MSDTC when used in an EF Code First app?

元气小坏坏 提交于 2019-12-01 02:17:03

问题


I have just introduced a TransactionScope use to an MVC3 app using EF 4.3 Code First, against a SQL 2010 Express local DB. When I try a SaveChanges inside the scope, I get a "Provider failed to open" notice, with an inner exception about a missing MSDTC. As far as I know, this should only occur if I used multiple connection strings. I only ever use connections to the one DB, I only have 1 string in the app. I do however use several DbContext instances, but only one across the transaction scope.

What can I do about resolving this?


回答1:


not sure if it's similar to this post? How to run two Entity Framework Contexts inside TransactionScope without MSDTC?

workarounds are:

0) Creating Promotable Transactions (depends on SQL server version?) (http://msdn.microsoft.com/en-us/library/ms172070.aspx)

1)Entity Framework – MSDTC Gotchya (http://joeknowsdotnet.wordpress.com/2012/07/19/entity-framework-msdtc-gotchya/)

2) Avoid unwanted Escalation to Distributed Transactions (http://petermeinl.wordpress.com/2011/03/13/avoiding-unwanted-escalation-to-distributed-transactions/)




回答2:


I think if you have a transaction scope and open two separate connections, even If they are to Same database with an identical connection string you'll find that th transaction will get promoted to a distributed transaction.

http://www.bomisofmab.com/blog/?p=184 adequately describes the situation.




回答3:


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).



来源:https://stackoverflow.com/questions/11451112/why-is-my-transactionscope-trying-to-use-msdtc-when-used-in-an-ef-code-first-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!