2016-07-06 22 views
1

我有兩個DbContexts(一個模型優先,另一個代碼優先)連接到MSSQL中的兩個不同的數據庫。 現在,當您從任何類中調用SaveChanges時,它都會使用TransactionScope類同時將數據寫入兩個數據庫。代碼如下所示。使用TransactionScope的一個事務中的多個數據庫(多個DbContexts)。

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

該問題在運行時引發。這是說,

型「System.Data.Entity.Core.EntityException」的異常出現在EntityFramework.SqlServer.dll但在用戶代碼

其他信息沒有處理:在底層提供對打開失敗。

內異常消息 - {「的夥伴事務管理器已禁止其爲遠程/網絡事務的支持(從HRESULT異常:0x8004D025)。」}

開機狀態MSDTC,並且沒有防火牆阻止MSDTC。 立即需要幫助。

+1

[在一個事務中的實體框架多個數據庫(可能的重複http://stackoverflow.com/questions/20468245/multiple -database-in-one-transaction-in-entity-framework) –

+0

參考:http://stackoverflow.com/questions/10130767/the-transaction-manager-has-disabled-its-support-for-remote-network-交易 – AllSpark

+0

已經提到這些,但沒有用。 AllSparl,David_L – zzzziiiirrrrkkkkkkkk

回答

1

按照問題下的評論,這個問題得到了解決:

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