Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 851
ProfK Avatar asked Jul 12 '12 11:07

ProfK


People also ask

Is MSDTC necessary?

Is MSDTC required? MSDTC is not required for Sql Server installation or operation. If you are only going to use Database Engine, then it is not required or used. If your Sql uses any of the above mentioned query techniques (Linked Server, OPENQUERY, etc), or SSIS or Workstation Components then MSDTC is required.

What MSDTC is used for?

The Microsoft Distributed Transaction Coordinator (MSDTC) service is a component of modern versions of Microsoft Windows that is responsible for coordinating transactions that span multiple resource managers, such as databases, message queues, and file systems.


2 Answers

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

like image 126
gan gary Avatar answered Oct 13 '22 07:10

gan gary


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

like image 33
dev.simond Avatar answered Oct 13 '22 06:10

dev.simond