Accessin a propery through a joined entity

Posts   
 
    
Darwin avatar
Darwin
User
Posts: 38
Joined: 12-Apr-2005
# Posted on: 07-Jul-2005 05:52:45   

I have three tables:

Group GroupUser User

GroupUser is a join to facilitate a many to many between Group and User.

In my GroupEntity I use a prefetch to fetch the GroupUserEntityCollection and the UserCollectionViaGroupUser

I tried to simply access the User Name with a GroupUser.User.Name reference (in a loop through each GroupUserEntity in the GroupUserCollection), but this errors out, appearing to not be initialized.

I know this is sparse info, I was hoping you could give some general direction around traversing through embedded entity collections. If that doesn't help me get my arms around it, I can provide more specific details tommorrow when I'm at work.

Thanks, Darwin

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 07-Jul-2005 09:53:49   

You should fetch the users in a subpath node of groupuser. I think you're now fetching the m:n relation 'Group.Users', and you're traversing the user references in groupuser. So instead of adding GroupEntity.PrefetchPathUsersViaGroupUser to the prefetch path, you should add GroupUserEntity.PrefetchPathUser to the SubPath of the groupusernode like this:


PrefetchPath2 path = new PrefetchPath2((int)EntityType.GroupEntity);
path.Add(GroupEntity.PrefetchPathGroupUserEntityCollection).SubPath.Add(GroupUserEntity.PrefetchPathUser);
// then fetch it

Frans Bouma | Lead developer LLBLGen Pro
Darwin avatar
Darwin
User
Posts: 38
Joined: 12-Apr-2005
# Posted on: 07-Jul-2005 22:38:45   

Yep, that worked!

As a point of learning, when would you use the PrefetchPathUsersViaGroupUser mechanism? It seems that you would almost always use the subpath route instead. Do I even need the PrefetchPathUsersViaGroupUser mechanism in my Group Entity to use the subpath under GroupUser?

Once again, great support.

Thanks, Darwin

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 08-Jul-2005 08:25:06   

Darwin wrote:

Yep, that worked!

As a point of learning, when would you use the PrefetchPathUsersViaGroupUser mechanism? It seems that you would almost always use the subpath route instead. Do I even need the PrefetchPathUsersViaGroupUser mechanism in my Group Entity to use the subpath under GroupUser?

If you want to pull just the m:n related entities and not really want to edit them, you can do that directly, like loading the field mapped onto the m:n relation. Otherwise you need the subpath indeed.

Once again, great support.

smile

Frans Bouma | Lead developer LLBLGen Pro