freddoo
freddoo

Reputation: 6858

subsonic 3 - The operation is not valid for the state of the transaction

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

Upvotes: 0

Views: 1113

Answers (2)

freddoo
freddoo

Reputation: 6858

it's a bug with subsonic 3.0.0.3

the fix can be found here issue 69

Upvotes: 1

Adam Cooper
Adam Cooper

Reputation: 8687

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

Upvotes: 1

Related Questions