2

I have got two DbContexts(One Model-first and the other one is code first) which connects to two different databases in MSSQL. Now when you call SaveChanges from any class, it writes data to both the databases at the same time using TransactionScope class. Code looks like below.

        using (TransactionScope scope = new TransactionScope())
        {
            using (Schema1Entities db1 = new Schema1Entities())
            {
                db1.SaveChanges();
            }
            using (Schema2Entities db2 = new Schema2Entities())
            {
                db2.SaveChanges();
            }
            scope.Complete();
        }

The problem raises during runtime. it is saying that

An exception of type 'System.Data.Entity.Core.EntityException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: The underlying provider failed on Open.

Inner-exception message - {"The partner transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0x8004D025)"}

Turned on MSDTC, and no firewall is blocking the MSDTC. Need help immediately.

Andrew
  • 7,602
  • 2
  • 34
  • 42
  • 1
    Possible duplicate of [Multiple database in one transaction in Entity Framework](http://stackoverflow.com/questions/20468245/multiple-database-in-one-transaction-in-entity-framework) – David L Jul 06 '16 at 23:45
  • refer: http://stackoverflow.com/questions/10130767/the-transaction-manager-has-disabled-its-support-for-remote-network-transactions – AllSpark Jul 06 '16 at 23:56
  • referred these already but no use. AllSparl, David_L – zzzziiiirrrrkkkkkkkk Jul 07 '16 at 00:09
  • If you don't use the `TransactionScope`, it saves the changed without any issue? Just a shot in the dark: did you try placing both `SaveChanges` in the same using block? (Both `using` lines next to each other). – Andrew Jul 07 '16 at 00:41
  • 1
    Thats awesome Andrew. Your dark shot worked. many thanks – zzzziiiirrrrkkkkkkkk Jul 07 '16 at 00:59
  • @zzzziiiirrrrkkkkkkkk, I just realized you never accepted my answer, LOL. – Andrew Jan 15 '20 at 16:54

1 Answers1

3

As per the comments under the question, this solved the issue:

using (TransactionScope scope = new TransactionScope())
{
    using (Schema1Entities db1 = new Schema1Entities())
    using (Schema2Entities db2 = new Schema2Entities())
    {
        db1.SaveChanges();
        db2.SaveChanges();
    }
    scope.Complete();
}
Andrew
  • 7,602
  • 2
  • 34
  • 42