Reputation: 4469
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?
Upvotes: 4
Views: 7762
Reputation: 3124
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));
Upvotes: 3