Entity Framework: Querying issue follow up.


So in THIS MIGHTY POST I asked why Entity Framework context likes to query the database despite the object (data row) already being loaded into the context. Well even though it’s not a perfect answer, Craig Stuntz had an answer at The O that gave some light to the situation:

context.SiteUser is an property of type ObjectQuery. When you execute an ObjectQuery, it will always hit the backing store. That’s what they do. If you don’t want to execute a database query, then don’t use an ObjectQuery.

Furthermore I could use GetObjectByKey:

GetObjectByKey tries to retrieve an object that has the specified EntityKey from the ObjectStateManager. If the object is currently not loaded into the object context, a query is executed in an attempt to return the object from the data source.

Which actually seems to me like how it should work naturally. After all, if it’s in the context and I am positive I don’t want to update the object from changes in the database, wouldn’t it make sense to not bother with the database at all? Seems like a wasted action. Again, I am guessing that the reason it does this is that it’s an all or nothing thing. It either does it all the time (IE query no matter the reason) or never. Grey area just doesn’t seem to fit Entity Framework design in this matter.