7

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?

ProfK
  • 49,207
  • 121
  • 399
  • 775

3 Answers3

4

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

Community
  • 1
  • 1
gan gary
  • 167
  • 1
  • 6
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).

dev.simond
  • 36
  • 1
  • This appears to be correct in EFCore anno 2020. I had DTC promotion for no reason I could find in a unitofwork instance-per-request architecture, and by adding a small query before the TransactionScope, it stopped promoting... – Thomas Mar 19 '20 at 16:08
0

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.

Xhalent
  • 3,914
  • 22
  • 21