4

I'm trying to use a TransactionScope at Domain level, so I can manipulate data across (potentially) several repositories, yet save all that in the same transaction.

I have the following save method:

public void Save(MyEntity source)
{
    using (var scope = new TransactionScope())
    {
        var context = new MyEFEntities(environment.ConnectionString);

        this.Repository.Add(source.ToDbMyEntity(), context);

        context.SaveChanges();

        scope.Complete();
    }
}

But I get the following error on the .SaveChanges():

The transaction specified for TransactionScope has a different IsolationLevel than the value requested for the scope. Parameter name: transactionOptions.IsolationLevel

What's causing this?

Spikee
  • 3,967
  • 7
  • 35
  • 68
  • Can you let me know the current `Isolation Level` in your DB? http://stackoverflow.com/questions/1038113/how-to-find-current-transaction-level – Alireza Nov 12 '15 at 14:47

1 Answers1

4

I think that default isolation level for Entity framework is the one which is default for the database, for instance default isolation level of SqlServer is READ COMMITTED, while for TransactionScope default is Serializable so when you try to change it inside using (var scope = new TransactionScope()) block it throws an error. Try something like this:

var transactionOptions = new TransactionOptions();
transactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
using (var scope = new TransactionScope(TransactionScopeOption.Required, transactionOptions));
Aleksa
  • 2,976
  • 4
  • 30
  • 49