views:

570

answers:

2

Hi

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

+1  A: 

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
freddoo
success it works, it's a real life saver
freddoo
@freddoo Can't find anything on that link you supplied...
Lieven Cardoen
+1  A: 

it's a bug with subsonic 3.0.0.3

the fix can be found here issue 69

freddoo

related questions