Retrieving related fields in a collection for 1:1 relationship

Posts   
 
    
IObject
User
Posts: 35
Joined: 06-Jul-2006
# Posted on: 11-Jul-2006 22:17:21   

I'm using the Adapter model and have two entities, Forms, and NavTrees, which is a 1:1 relationship. I retrieve the Forms entity collection and need to also retrieve the related fields from each of the NavTrees. The NavTrees are not getting retrieved with the collection of Forms, and I'm stumped on how to get them to populate. I've checked the forum and the documentation and haven't found anything that specifically addresses this. Can someone point me in the right direction?

using llbl v2

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 12-Jul-2006 02:25:33   

You will want to use a prefetch path that defines which other entities to fetch when you fetch Forms. You can read more about this under Generated code - Prefetch paths, Adapter in the manual.

Here's a quick example.


EntityCollection forms = new EntityCollection(new FormEntityFactory());
IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.FormEntity);
prefetchPath.Add(FormEntity.PrefetchPathNavTree);
IRelationPredicateBucket filter = new RelationPredicateBucket();
filter.PredicateExpression.Add(FormFields.FormId == 2));
DataAccessAdapter adapter = new DataAccessAdapter();
adapter.FetchEntityCollection(forms, filter, prefetchPath);

now forms[0].NavTree would be the related NavTree entity.

IObject
User
Posts: 35
Joined: 06-Jul-2006
# Posted on: 12-Jul-2006 17:49:00   

Thanks for the help, but something is still not working properly.

Here is the code I used:


EntityCollection forms = new EntityCollection(new FormEntityFactory());
IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.FormEntity);
prefetchPath.Add(FormEntity.PrefetchPathNavTree);
DataAccessAdapter adapter = new DataAccessAdapter();
adapter.FetchEntityCollection(forms, null, prefetchPath);

The forms[x].NavTree are still coming up null. Does the 1:1 relationship have to exist on both sides for this to work?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 12-Jul-2006 17:56:00   

I think the problem is how you access the Form.NavTree object.

You should cast each element of the entity collection to the appropriate type before accessing the members of that type.

((FormEntity)forms[x]).NavTree 
IObject
User
Posts: 35
Joined: 06-Jul-2006
# Posted on: 12-Jul-2006 18:06:44   

I should have specified I was casting it to the FormEntity type, ((FormEntity)forms[x]).NavTree. I see whats happening now. The Form table can have more FormID's than the NavTree has FormID's and its retrieving all the Forms, reguardless if there are matching FormID's in the NavTree table. i.e.


Form   NavTree
1        1
2        2
3        5
4
5

So I need to do a join on this, which is what I thought having the relationship would do. If the relationship doesn't take care of this, what does?

IObject
User
Posts: 35
Joined: 06-Jul-2006
# Posted on: 12-Jul-2006 18:35:01   

Fixed, had the 1:1 relationship on the wrong side.