Multiple stored procedure calls in 1 transaction with Entity Framework 5

2

I am using Entity Framework 5 and need to make multiple stored procedure calls in a single transaction. They are all inserts where each depends on an output of the previous one and I want to be able to roll everything back in case one fails. Each call uses the same Entities object. The first call executes successfully, but the next one always fails with the following error:

"The underlying provider failed on Open."

Inner exception: "Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool."

Inner Inner exception: "The transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0x8004D024)"

Is there a way to do this without going down the DTC road?

Code:

ObjectParameter outputParam1 = new ObjectParameter("newEntity1", 0);
ObjectParameter outputParam2 = new ObjectParameter("newEntity2", 0);

using (var scope = new TransactionScope())
{
    try
    {
        using(var context = new DatabaseContext())
        {
            context.f_createEntity1(outputParam1);
        }

        using(var context = new DatabaseContext())
        {
            context.f_createEntity2(ouputParam2, outputParam1.Value);
        }

        ... other calls ...

        scope.Complete();
    }
    catch (Exception ex)
    {
        ... handle error ...
    }
}
c#
entity-framework
c#-4.0
asp.net-mvc-4
entity-framework-5
asked on Stack Overflow Aug 28, 2012 by Ross

1 Answer

1

There's not a way to do this without having the DTC enabled.

From a related SO post:

At least two durable resources that support single-phase notifications are enlisted in the transaction. For example, enlisting a single connection with does not cause a transaction to be promoted. However, whenever you open a second connection to a database causing the database to enlist, the System.Transactions infrastructure detects that it is the second durable resource in the transaction, and escalates it to an MSDTC transaction.

answered on Stack Overflow Aug 28, 2012 by Mark Oreta • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0