代码之家  ›  专栏  ›  技术社区  ›  freddoo

子音速3-该操作对事务状态无效

  •  0
  • freddoo  · 技术社区  · 17 年前

    我正在尝试以下代码

    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 回复  |  直到 17 年前
        1
  •  1
  •   Adam Cooper    17 年前

    您需要将TransactionScope包装在sharedbConnectionScope中,请参见 here 详情。以下内容适用于您的示例

    using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope()){
    {
      using (TransactionScope tc = new TransactionScope()) 
      {
        uAddress.Save();
        ud.AddressId = uAddress.Id;
        ud.Save(); // error is here
        tc.Complete();
      }
    }
    
        2
  •  1
  •   freddoo    17 年前

    这是一个亚音速3.0.0.3的缺陷

    修复可以在这里找到 issue 69

    推荐文章