Do entities share related entities?

Posts   
 
    
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 15-Apr-2008 12:24:04   

Hi,

If I request a collection and do a prefetch for each item and all of the items in the collection reference the same entity, is there one copy of the related entity in memory for each item or do they all reference the same instance?

Cheers, Ian.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 15-Apr-2008 12:31:24   

They don't reference the same entity, unless you use a Context to keep one instance only in memory.

Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 11-Sep-2008 18:03:34   

So if I do this...


            Context context = new Context();

            adapter.FetchEntity(accountEntity, path2, context);

Is that all it takes to make sure every item returned is only loaded once?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 12-Sep-2008 05:26:55   

Yep. But remember to include the subPath. For example:

Context c = new Context();
EntityCollection<OrderEntity> orders = new EntityCollection<OrderEntity>();

IPrefetchPath2 path = new PrefetchPath2((int) EntityType.OrderEntity);
path.Add(OrderEntity.PrefetchPathCustomer);

IRelationPredicateBucket filter = new RelationPredicateBucket();
filter.PredicateExpression.Add(OrderFields.CustomerId == "ALFKI");

c.Add(orders);

using (DataAccessAdapter adapter = new DataAccessAdapter())
{
    adapter.FetchEntityCollection(orders, filter, path);
}

Here the customer 'ALFKI' associated to all these orders is the same instance.

David Elizondo | LLBLGen Support Team