-1

Does "IsolationLevel.ReadUncommitted" in EF read from CACHE?

i have a transaction scope with IsolationLevel.ReadUncommitted

Class Orders{
     public int Id {get();set();}
     public int OrderType {get();set();}
}   
While(true){
     var transactionOptions = new TransactionOptions
     {
          IsolationLevel = IsolationLevel.ReadUncommitted
     };

     using(TransactionScope(TransactionScopeOption.Required, transactionOptions))
     {
          var temp = context.Orders.Include(e => e.Customer).SingleOrDefault(e => e.Id == 1);
     }

}

Suppose, initially, i have one order with order type "1", temp gets initialised to that one row. If i were to update the orderType of my order to "2" using an Update query in SSMS (MSSQL), the order type of the object pulled from context is still "1" although in DB its "2". Is this because of Read UnCommitted?

Anirudh
  • 11,599
  • 2
  • 10
  • 26
  • Did you commit the `type=2` update? Uncommitted would let you read an uncommitted '2', reading a '1' points to some other problem. Using a higher TransIsolation should show the same results. – Henk Holterman Sep 05 '14 at 08:14

3 Answers3

2

This has nothing to do with read uncommited.read uncommited is an isolation level that indicates how to handle locks in transactions.

Your problem has to do with how EF works. EF always caches the data, and if it's available, get it from the cache. No matter what the isolation level is.

If you need fresh data, you have to manually refresh it.

if you google "refresh entity framework cache" you'll see a lot of ways to manage this. As you don't specify the version of EF, and the technology used (DbContext? ObjectContext?) I cannot give a more specific answer.

By the way, take into account that DataContexts are not expected to have a very long life. If you continue using the same DbContext for a long time it will end up taking a lot of memory to hold all the cached data. You should use fresh DbContexts most of the time. (It alswo depends on the kind of app, it's not he same for ASP.NET or WPF, for example)

JotaBe
  • 34,736
  • 7
  • 85
  • 109
0

You can specify MergeOption.OverwriteChanges;

result.MergeOption = MergeOption.OverwriteChanges;

or use ObjectContext.Refresh

or disable the object tracking on the context

Community
  • 1
  • 1
Sameer
  • 2,764
  • 4
  • 24
  • 48
0

i chose to use

context.Entry(orders).Reload(); 

reloads the entity

Reload MSDN

Anirudh
  • 11,599
  • 2
  • 10
  • 26