Mel
Mel

Reputation: 3128

EF4 Code First: how to create a transaction and change isolation level

I am using the EF4 code first for db access. I needed to wrap some changes in a transaction and change the isolation level to repeatable read. It would be a cinch to write this in SQL, but EF is giving me a hard time. Is there any way to accomplish this?

Upvotes: 1

Views: 951

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364369

You must use TransactionScope:

using (var scope = new TransactionScope(TransactionScopeOption.Required, 
       new TransactionOptions { IsolationLevel = IsolationLevel.RepetableRead} ))
{
    // Grab data
    // Process changes
    context.SaveChanges();
    scope.Complete();
}

Upvotes: 2

Related Questions