subsonic 3 - 该操作对交易状态无效

时间:2009-08-18 04:14:57

标签: subsonic3 transactionscope

我正在尝试以下代码

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与数据库断开连接?

感谢

2 个答案:

答案 0 :(得分: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();
  }
}

答案 1 :(得分:1)

这是亚音速3.0.0.3的错误

可以在issue 69

找到修复程序