Up and then back down a prefetch path

Posts   
 
    
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 18-Dec-2006 01:21:27   

Hi,

I have a db structure like this...

1) Messages. Each message has a sender (inline) which references a user. 2) Internal recipients. Each row references a message and a user 3) External recipients. Each row references a message. 4) Users.

Given an internal recipient ID, I want to load the recipient row and then prefetch a) the message b) the user who sent the message c) all internal recipients d) all internal recipient users e) all external recipients

I tried this but it said I'd already added a prefetch once all ready..


            IPrefetchPath2 path2 = new PrefetchPath2((int)EntityType.MessageInternalRecipientEntity);
            path2.Add(MessageInternalRecipientEntity.PrefetchPathMessage).SubPath.Add( MessageEntity.PrefetchPathMessageInternalRecipient).SubPath.Add(MessageEntity.PrefetchPathUser);
            path2.Add(MessageInternalRecipientEntity.PrefetchPathMessage).SubPath.Add( MessageEntity.PrefetchPathMessageExternalRecipient);
            path2.Add(MessageInternalRecipientEntity.PrefetchPathMessage).SubPath.Add(MessageEntity.PrefetchPathUser);

I hope you can see what I'm trying to do. Given the fetched MessageInternalRecipientEntity, get the message and then prefetch the info related to the message.

How do I get this to work?

Cheers,

Ian.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Dec-2006 08:10:47   

I'm somehow confused about your database structure, would be clearer for me if you posted tables structure.

Anyway, the problem is that you have add the same prefetchPathElement more than once:

path2.Add(MessageInternalRecipientEntity.PrefetchPathMessage)...

If you wanted to add multiple SubPaths, use a PrefetchPathElement as follows:


PrefetchPath2 path2 = new PrefetchPath2((int)EntityType.MessageInternalRecipientEntity);

PrefetchPathElement2 prefetchPathElement = MessageInternalRecipientEntity.PrefetchPathMessage;

prefetchPathElement.SubPath.Add(MessageEntity.PrefetchPathMessageInternalRecipient).SubPath.Add( MessageEntity.PrefetchPathUser);

prefetchPathElement.SubPath.Add(MessageEntity.PrefetchPathMessageExternalRecipient);

prefetchPathElement.SubPath.Add(MessageEntity.PrefetchPathUser);

path2.Add(prefetchPathElement);

Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 18-Dec-2006 17:57:03   

Hi there,

With your example I was able to get this to work...


            PrefetchPath2 path2 = new PrefetchPath2((int)EntityType.MessageInternalRecipientEntity);
            
            IPrefetchPathElement2 prefetchPathElement = MessageInternalRecipientEntity.PrefetchPathMessage;
            prefetchPathElement.SubPath.Add(MessageEntity.PrefetchPathMessageInternalRecipient).SubPath.Add( MessageInternalRecipientEntity.PrefetchPathUser);
            prefetchPathElement.SubPath.Add(MessageEntity.PrefetchPathMessageExternalRecipient);
            prefetchPathElement.SubPath.Add(MessageEntity.PrefetchPathUser);
            
            path2.Add(prefetchPathElement);
            path2.Add(MessageInternalRecipientEntity.PrefetchPathUser);

So here is a better description of the tables....

Users UserID PK

Messages MessageID PK UserID FK (Sender)

Internal Recipients InternalRecipientID PK MessageID FK UserID FK DateViewed

ExternalRecipients ExternalRecipientID PK MessageID FK EmailAddress

Now, when I'm showing a user's inbox, I can't link off it to the message detail page using the message's PK because a user may have been sent the same message twice. Once, say, as the primary recipient and then also as a Cc.

So at the message detail page, when I need to update the message's DateViewed column for the user, I need to know the InternalRecipientID in order to update the right one.

So I'm loading the Internal Recipient entity and then prefetching the message and related data. Trouble with the code above is that its getting the same Internal Recipient entity twice. Once for the main fetch and once for the prefetch.

I suppose I could filter it out from the prefetch or maybe do two different fetches - one for the recipient entity and then look up the message seperately. Or I could just pass the messageID along with the InternalRecipientID from the inbox. That's probably simpler.

Ian.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 19-Dec-2006 09:02:46   

The question is, why do you want to fetch the Internal recepients un the prefetchPath when you are fetching the InternalRecpient Entity in the Main fetch?

Do you need the other internal recpients in your business logic in this particular case?

And if yes, then it's a "Yes" you can simply filter out the main recepient from the prefetched ones:

prefetchPathElement.SubPath.Add(MessageEntity.PrefetchPathMessageInternalRecipient).SubPath.Add( MessageInternalRecipientEntity.PrefetchPathUser, 0, new PredicateExpression(InternalRecipientFields.InternalRecipientID != MainRecepientId));

Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 19-Dec-2006 21:17:43   

Hi Walaa,

Do you need the other internal recpients in your business logic in this particular case?

Yes I want to list the cc's as Outlook Express does.

Your filter would do the trick. However, I've decided to send both the internal recipient pk and the message pk to the message detail page anyway. So all I have to do is fetch the message and prefetch everything else and then upon binding and iterating over the message's recipients, I can match one with the one sent with the request and update its dateviewed value if necessary.

Cheers,

Ian.