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

I just imagined using TransactionScope for an MVC3 application using EF 4.3 Code First, against the local SQL 2010 Express database. When I try to SaveChanges inside an area, I get a “Provider could not open” notification with an internal exception due to the lack of MSDTC. As far as I know, this should only happen if I used multiple connection strings. I use only connections to one database, I have only 1 row in the application. However, I use several instances of DbContext, but only one in the transaction area.

What can I do to solve this problem?

+4
source share
3 answers

Not sure if this looks like this post? How to run two Entity Framework contexts inside a TransactionScope without MSDTC?

workarounds:

0) Creation of advertising transactions (depends on the version of the SQL server?) ( 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 into distributed transactions ( http://petermeinl.wordpress.com/2011/03/13/avoiding-unwanted-escalation-to-distributed-transactions/ )

+3
source

I think that if you have a transaction area and open two separate connections, even if they belong to the same database with the same connection string, you will find that the transaction will be distributed to the distributed transaction.

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

0
source

The first time the application accesses EF, it will perform initialization (it will build db if it does not exist, etc.). If this first call is inside a TransactionScope, it will facilitate DTC.

To fix this, make an EF call in the application launch method, and this will ensure that the EF is initialized outside of TransactionScope. From here, you can include one or more calls in the EF inside TransactionScope and will not facilitate DTC by providing: a) you always use EXACTLY the same connection string, and b) you use Sql Server 2008 and above (which you are).

0
source

All Articles