Cannot get context to work

Posts   
 
    
tuppers
User
Posts: 16
Joined: 09-Feb-2009
# Posted on: 24-Feb-2009 14:41:03   

Hi,

Hopefully someone can help as I have either misunderstood the concept of the Context facility or cannot get it to work.

My understanding is that if you request an entity twice using the same primary key from a context instance it should only hit the DB once to get field data. This is because the second request into the context should reuse the entity instance from the first request therefore removing the need for a second DB hit. Is this correct?

Assuming it is am I doing something wrong in my code as both samples below carry out 2 DB hits:

Context context = new Context();
ClubEntity entity1 = (ClubEntity)context.Get(new ClubEntity(218147));
ClubEntity entity2 = (ClubEntity)context.Get(new ClubEntity(218147));

Context context = new Context();

ClubEntity entity = new ClubEntity();
entity.FetchUsingPK(218147, null, context);

ClubEntity entity2 = new ClubEntity();
entity2.FetchUsingPK(218147, null, context);

Many Thanks.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 24-Feb-2009 21:29:41   

From the manual

The Context objects don't act as a cache which is used to prevent database activity. Every query is still executed on the database. If an entity is already loaded in the used Context object, the entity data is not added to a new entity object, but the entity object already loaded is updated. If the already loaded object is dirty, the data isn't updated and the loaded entity data is simply skipped, and the already loaded entity object is returned as is. This is done in the Get routine of the Context object. The Context has a flag to disallow this particular action: SetExistingEntityFieldsInGet. See the LLBLGen Pro reference manual for details on this flag.