Entity Frameworks really likes distributed transactions. If you're using an Database Object Context inside a TransactionScope, Entity Framework will happily spawn distributed transactions, causing deployment pain and security holes.

How to avoid Entity Framework to spawn a Distributed Transaction

A little Background

This problem blowed in my face at the same time that the problem described in my last post so I won't describe the scenario, go on read it there, it's like one paragraph long you lazy programmer bastard.

The problem

As I said before, I had a long transaction which involved making several db CRUD operations (and saving changes amongst them). I also took care of using the same Database Object Context ("so Entity Framework gets a hint that it's only one transaction!" I naively said to myself). Anyway, when I deployed my solution, I was greeted by a beautiful error talking about distributed transactions, security and Windows Credentials.

"I don't wanna get it there, hommie"

I said to myself, not calmly.

What is happening?

Entity Framework does a lot so you don't have to worry about common, mundane stuff. In this efforst it sometimes goes straight and drive to the road that takes you directly to Belsevuv's door.

In this case, Entity Framework can't be sure you're using the same databse connection in all your operations even if you are using the same Database Object Context. So it will assume you're actually spawning several database connections.

Not my problem

"A database transaction spawning several database connections? That's no job for me mate, but I know this guy, Distributed Transaction Coordinator (DTC as we call him around the block) who will do it happily"

Says Entity Framework. Only that DTC is not a nice guy, and he won't do it happily. He can't. He must watch that everything is secury and you, dumb programmer or your dumb users don't mess around with his server.

The solution

Remember when I said that I tried to hint Entity Framework about me using the same database connection? It turns out it can be done, I was just half-doing it.

So, the thing that I missed was telling it about the single Database connection. This is done simply by openning the connection yourself after creating the context. Here, let me show you how I do it in Entity Framework.

using (var ctx = new MyEntities())
{
    ((IObjectContextAdapter)ctx).ObjectContext.Connection.Open();
    ....
    ...all my neat stuff
}

I haven't found a drawback yet.

Posted by: fabzter
Last revised: 11 Nov, 2013 02:56 PM History

Comments

OOOO
OOOO
08 Feb, 2016 09:33 AM

SD

Georgette
Georgette
17 Aug, 2016 06:21 AM

Your article How to avoid Entity Framework to spawn a Distributed Transaction was really informative and helpful for the programers. It shares the easy techniques to avoid such errors and face mask is a big time help for the beginners. Thank you.

14 Dec, 2016 07:55 AM

Thanks for share the wonderful solution. I just would like to say you have shared very quality stuff in your content. This was quite new information for me. Thanks for sharing this with me. get discounted bottle packaging boxes

Your Comments

Used for your gravatar. Not required. Will not be public.
Posting code? Indent it by four spaces to make it look nice. Learn more about Markdown.

Preview