0

I'm trying the following code

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();
}

when i reach ud.save() i get the error 'The operation is not valid for the state of the transaction. ---> System.Transactions.TransactionPromotionException: Failure while attempting to promote transaction'

if i comment out the transaction part it works fine, isn't .SingleOrDefault disconnecting from the db ?

thanks

freddoo
  • 6,770
  • 2
  • 29
  • 39

2 Answers2

1

You need to wrap your TransactionScope in a SharedDbConnectionScope, see here for details. The following should work for your example

using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope()){
{
  using (TransactionScope tc = new TransactionScope()) 
  {
    uAddress.Save();
    ud.AddressId = uAddress.Id;
    ud.Save(); // error is here
    tc.Complete();
  }
}
Adam Cooper
  • 8,077
  • 2
  • 33
  • 51
  • that is a very good point but I still have the problem seems like a bug is subsonic 3.0 i will give this solution a try http://github.com/subsonic/SubSonic-3.0/issuesearch?state=open&q=SharedDbConnectionScope#issue/69 – freddoo Aug 19 '09 at 00:21
1

it's a bug with subsonic 3.0.0.3

the fix can be found here issue 69

freddoo
  • 6,770
  • 2
  • 29
  • 39