Entity Framework: Retrieving newly inserted context objects



The issue I'm experiencing with "re-finding" recently generated objects while utilizing the entity framework is roughly as follows:

string theId = "someId";

private void Test()
  using(MyEntities entities = new MyEntities())
    EntityObject o = new EntityObject();
    o.Id = theId;

void CallSomeOtherMethod(MyEntities ents)
  EntityObject search = ents.EntityObject.FirstOrDefault(o => o.Id == theId);
  if(search == null) 
    Console.WriteLine("wha happened???");

(no promise that the code will function, incidentally; it's all in my mind.)

Why can't the query "discover" the newly formed EntityObject?

It works (which doesn't surprise me) if I use SaveChanges() after AddToEntityObject, but why doesn't it correctly pull from the cache?

Since I'm still new at this, I'm hoping that there's anything extremely simple that I'm simply missing.


4/19/2009 7:23:24 AM

Accepted Answer

Ents are to blame for this. EntityObject. The datasource is always queried by WhatEver. This was chosen during design. They do it this manner rather than the alternative, which would require them to run the query against both the datasource and the local cache before merging the results. They were unable to manage this consistently, as one of the engineers noted in a blog post (I can't recall where precisely).

As you can imagine there are a lot of corner an edge cases you have to handle properly. You may just search the database for an ID that you or another user established locally. You would have to be ready to manage conflicts on (nearly) every query as a result of this. Perhaps they could have created ways to query the local cache and the datasource, but that would not have been very wise either.

Consider looking at Lazy loading with transparency for Entity Framework. This takes the place of the standard code generator, giving you entities that automatically fill their linked entity collections and entity references upon access. By doing so, all the

if (!Entity.ReleatedEntities.IsLoaded)

pieces of code. Additionally, because the collections are always implicitly loaded, you may query them. But this answer is also not ideal. There are a few problems. The function cannot obtain the associated entities from the database, thus if you create a new object and then try to access a collection of related entities, you will receive an error. Data binding is a problem, and there could be more that I am not aware of.

The benefit is that you can address the problems yourself since you have access to the source code. If I have time, I'll go at the first problem in more detail. But I am certain that it won't be that simple to solve, since I anticipate some instances when the object would simply not touch the database if it has just been created, which is not the anticipated behavior.

11/1/2015 12:40:15 PM

Popular Answer

Since it hasn't yet been persistent in the database, the just added item is in the local DataSource.

hence, you might say:

EntityObject search = ents.EntityObject.FirstOrDefault(o => o.Id == theId) ??
                      ents.EntityObject.Local.FirstOrDefault(o => o.Id == theId);

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow