Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
No replies
SystemAdmin
SystemAdmin
2826 Posts
ACCEPTED ANSWER

Pinned topic Using Non-Distributed Transactions in Entity Framework 5?

‏2013-02-06T18:46:18Z |
I am starting out learning all of this. This is my code:

using (var context = new developContext())
{
context.Database.Connection.Open();
using (TransactionScope transaction = new TransactionScope())
{
consecs c = context.consecs.First(i => i.consec >= -1);
consecs c2 = context.consecs.First(i => i.consec >= -1);
transaction.Dispose();

}
}

I open the connection before the transaction because if I do this the other way around, it escalates to a distributed transaction from the beginning. Then I issue the first query. I just wanted to get the only record in the table with only one field, so I used that EF syntax for it.

The problem is, this select closes the connection when it's done, so the second select tries to reopen the connection with a distributed transaction, and fails.

The example I was going for with transactions working, is:
update consecs set consec = consec+1; //update and lock the table
select consec from consecs; // should be the updated value
rollback; // undo all of this so the example is repeatable without altering the database

But I am getting stuck with trying to keep the transaction local, even if I'm not making an update yet.

Any tips?