0

Quick (I think) question about how Entity Framework participates in a DTC Transaction.

If I create two DbContexts within the same distributed transaction, will data I save in the first context be available to subsequent queries via the second context?

In other words, within a DTC transaction: I fetch, change and save data via context1, then create context2 and query the same entities. Will the uncommitted data from context1 be available to context2?

Phil Sandler
  • 27,544
  • 21
  • 86
  • 147

2 Answers2

0

That depends on the isolation level you're using.

If you were to enclose your DbContexts in a TransactionScope and specifiy IsolationLevel.ReadUncommitted, e.g.

var options = new TransactionOptions{ IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted };
using(var scope= new TransactionScope(TransactionScopeOption.Required, options))
{
    ... DbContexts here

    scope.Complete();
}

Then your second query will be able to see the changes made by the first. For other isolation levels they won't.

The default isolation level for SQL Server is Read Committed, but for TransactionScope is Serializable.

See http://www.gavindraper.co.uk/2012/02/18/sql-server-isolation-levels-by-example/ for some examples.

Phil
  • 42,255
  • 9
  • 100
  • 100
0

It appears that the second context can indeed see the changes committed by the first. We are using read committed isolation. I am essentially doing (pseudo code):

Start Distributed Transaction

Context1: Fetch data into entities
Context1: Change entities
Context1: Save entities

Context2: Fetch data into same entities as used context 1
Context2: Change entities
Context2: Save entities

Commit Distributed Transaction

When I fetch into context 2, I do indeed see the changes that were saved in context 1, even though they are not committed (via DTC).

Phil Sandler
  • 27,544
  • 21
  • 86
  • 147