2009-08-18 50 views
0

我想下面的代碼亞音速3 - 操作是無效的事務的狀態

UserDetail ud = UserDetail.SingleOrDefault(u => u.UserName == CurrentUserName); 
    if (ud == null) 
    ud = new UserDetail(); 

Address uAddress = ud.AddressId.HasValue 
        ? Address.SingleOrNew(a => a.Id == ud.AddressId) 
        : new Address(); 

using (TransactionScope tc = new TransactionScope()) 
{ 
    uAddress.Save(); 
    ud.AddressId = uAddress.Id; 
    ud.Save(); // error is here 
    tc.Complete(); 
} 

當我到達ud.save()我得到錯誤「的操作是無效的交易狀態。 ---> System.Transactions.TransactionPromotionException:嘗試提升交易時失敗'

如果我註釋掉交易部分,它工作正常,不是.SingleOrDefault從數據庫斷開?

感謝

回答

1

你需要用你的TransactionScope在SharedDbConnectionScope,見here瞭解詳情。以下應爲你的榜樣

using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope()){ 
{ 
    using (TransactionScope tc = new TransactionScope()) 
    { 
    uAddress.Save(); 
    ud.AddressId = uAddress.Id; 
    ud.Save(); // error is here 
    tc.Complete(); 
    } 
} 
+0

這是一個非常好的問題,但我仍然有問題,好像一個錯誤是亞音速3.0 我會給這個解決方案嘗試 http://github.com/subsonic/SubSonic-3.0/issuesearch?state=open&q=SharedDbConnectionScope#issue/69 – freddoo 2009-08-19 00:21:46

+0

成功的工作,這是一個真正的生活救星 – freddoo 2009-08-19 00:50:10

+0

@freddoo在你提供的鏈接上找不到任何東西...... – 2009-12-15 15:35:58

1

工作,它與亞音速3.0.0.3

一個bug修復可以在這裏找到issue 69