2

I read the following StackOverflow posts about nested transactionScope, but I couldn't get it to work in one of my integration tests. I wanted to insert some records into the database and verify the writes afterward immediately, and then rollback at the end of the test to its origin.

The problem is that I would need to explicitly call connection.EnlistTransaction(Transaction.Current) in order to have it working. One thing that I am not sure about is whether everything has to be in the same layer or file, because in my case (code example on the bottom) the transaction scope is defined in the service tier, and it calls multiple downstream repository objects for low level SQL query operations (CRUD)

Here's the code I've had so far

using(TransactionScope scope = new TransactionScope())
{
   repository1.CreateEntries(expectedList);
   repository2.UpdateEntries(expectedList);

   scope.Dispose();
}

this is from repository1 class

void CreateEntries(List<Object> list) {
   using (var conn = new SqlConection(CONNECTION_STRING))
   {
            conn.Open();

            // i need to explicitly call this
            conn.EnlistTransaction(Transaction.Current);
            ....
   }
}

this is from repository2 class

void UpdateEntries(List<Object> list) {
   using (var conn = new SqlConection(CONNECTION_STRING))
   {
            conn.Open();

            conn.EnlistTransaction(Transaction.Current);
            ....
   }
}
peter
  • 8,333
  • 17
  • 71
  • 94
  • I know this is an old post, but did you figure this one out? May it's "Enlist=false" on a connection string? (https://stackoverflow.com/questions/35716193/what-is-enlist-false-means-in-connection-string-for-sql-server) – Eugene D. Gubenkov Dec 26 '18 at 12:59

0 Answers0