Bug #59996 old entity is returned, not real data from DB
Submitted: 7 Feb 2011 20:17 Modified: 2 Mar 2011 17:45
Reporter: saverio mosca Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:6.3.5 OS:Windows (7 home premium/fx4.0)
Assigned to: CPU Architecture:Any
Tags: entity framework, linq to entities

[7 Feb 2011 20:17] saverio mosca
Description:
During developement of my current application (c# fw4.0), I realized following bad behaviour of 6.3.5 connectors.

It appears that if an object context is not reinstantiated as a new object just before a linq select, the data entity returned DOES NOT REFLECT data on the DB, but it reports OLD data.

In few words, the following example doesn't works and reports old data (the first data read by the application) :

lastupdate l = (from c in GV.EntityPer.lastupdate
select c).First(); 

The object context EntityPer is instantiated in another visible static class GV, and is simply used here. Into the 'l' class, I find OLD data, not reflecting the DB ( seems like cached data...).
- - - - - -
The following works fine :

pEntities Ent = new pEntities();
lastupdate l = (from c in Ent.lastupdate
select c).First();

The object context is re-created right before linq select.

This is the first bug I am reporting. I don't know if the working method is more recommended than the first, but I expect some errors/exception if something is wrong, and not just obsolete data, as this is very dangerous.
Interesting to note, if I use anonymous ( select new {c.aaaa, c.bbb, c.ddd} ), also the first method works fine, it just fails requesting all the entity.
Thanks to anyone can see this,
Saverio

How to repeat:
Just instantiate an object context only once in an application, and use that.

Suggested fix:
Don't know. My workaround was to reinstantiate a new context for every linq query.
[15 Feb 2011 16:13] Richard Deeming
This is a feature of the Entity Framework known as identity resolution. You can change the MergeOption property for the query to override this behaviour.

"The default is MergeOption.AppendOnly ... new entities retrieved by the query are attached to the ObjectContext, and if an entity which has the same key as an incoming entity is already attached to the context, then that object is returned as is rather than the incoming entity."

http://blogs.msdn.com/b/dsimmons/archive/2010/01/12/ef-merge-options-and-compiled-queries....
[18 Feb 2011 14:56] saverio mosca
Thanks Richard, I really didn't know such property..never heard. But the article on your link is well explained.

Thanks for your time, Sav