Entity Framework: Why does the database get hit if the data is in the context?

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 It sounds like what you really want is a function which says, "If the entity is already materialized in the context, then just return it. If it isn't, then go grab it from the database."

As it happens, ObjectContext includes such a function, called 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.

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. It sounds like what you really want is a function which says, "If the entity is already materialized in the context, then just return it.

If it isn't, then go grab it from the database. " As it happens, ObjectContext includes such a function, called 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.

Hrm seems odd that it wouldn't do this by default. I guess the fact that the default MergeOption says not to look at the database for changes, but only holds that in the context of overwriting is annoying. Then again, it is called MergeOption... – Programmin Tool Feb 19 '09 at 15:38.

IMO, the reason that EF hits the database a second time is to make sure that there aren't any additional rows in the db that satisfy the query. It's possible that additional relevant rows have been inserted into the table since the first query was issued, and EF is seeing if any of those exist.

If I understand your question, this should answer it: Actually, the way the entity framework does this by default is to require notifications of changes from the entity objects to a framework class called the state manager which then keeps track of which properties have been changed. The original values are copied only on demand. When updates happen, those original values are used in talking to the server only if the changed properties are marked as “concurrency tokens”.

That is, for any concurrency token columns, when the framework is creating an update statement it will include a check to verify that the row in the database still has the original value, and if not it will raise an exception to notify the program that someone else has changed the row in the database. It’s also true that the entity framework doesn’t absolutely require notifications from the property setters, you can also determine what’s modified in the application code and call an explicit method on the framework to indicate which properties are changed (but then the framework will only have a record that the property is modified, it won’t have an original value). Which comes from here.

More can be read about it here, and here. Edited to add: It appears that with EF, there is an ObjectStateManager that tracks changes which never really allows for disconnected data. In order to have disconnected data, you'll have to call the ObjectContext.

Detach method to disconnect your object. More can be found here and here.

The only thing that seems off is that this didn't involve an update. Now I can either be incredibly thick (Very possible) or I can assume that it by default ALWAYs checks the database regardless of a select, udpate, or whatever. Is that what I should assume?

– Programmin Tool Feb 16 '09 at 15:37 The links lead me to something that I have updated my original post with but unfortunately only add to my confusion of what's going on. – Programmin Tool Feb 16 '09 at 16:35.

Nope. First (Top) still hits it every time the linq "query" is run. – Programmin Tool Feb 19 '09 at 14:54 did you do context.SiteUser.

FirstOrDefault(role => role. UserID == 1)? I'm wondering what would happen if you retrieved to otherUser and than compared the data in someUser to otherUser?

– Davy Landman Feb 19 '09 at 15:03 Values are still the same even if you somehow change them in the database between hydrating someUser and otherUser. I suspect this is because it knows it's in the context and the MergeOption by default says to ignore changes. – Programmin Tool Feb 19 '09 at 15:41 If I'm not mistaken you'll get the same reference (because you're in the same context).

– Davy Landman Feb 19 '09 at 15:46 One can hope. If not, there might be something scary wrong with Entity Framework. – Programmin Tool Feb 19 '09 at 18:01.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions