2

我使用實體框架5並需要在一個事務中多次調用存儲過程調用。他們是所有插入其中每個依賴於前一個的輸出,我想能夠滾動一切恢復的情況下,一個失敗。每次調用使用相同的實體對象。第一次調用執行成功,但下一個總是失敗,出現以下錯誤:「基礎提供失敗的打開」多個存儲過程在1個交易與實體框架5

內部異常:「對於分佈式事務管理網絡訪問(MSDTC)已被禁用,請啓用DTC在使用MSDTC組件服務管理工具的安全配置網絡訪問」

裏面的內異常:「事務管理器已禁用其遠程/網絡事務的支持(從HRESULT異常:0x8004D024)。」

有沒有辦法做到這一點不下去了DTC路?

代碼:

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 ... 
    } 
} 

回答

1

有沒有辦法做到這一點,而無需啓用DTC。

從相關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.