views:

128

answers:

1

I am having a very tough time with this one. I have a Navigation property called Attachment that is part of an entity called ContentChannel. ContentChannel is a many to one relationship with KioskType.

In my domain service extension class, I have the following query:

public IQueryable<ContentChannel> GetContentChannelFromKioskType( long kioskTypeID )
{
    var returnSet = (from cc in ObjectContext.ContentChannels.Include( "Attachment" )
                   join pcc in ObjectContext.PublishedContentChannels on cc.ContentChannelID equals pcc.ContentChannelID
                        where pcc.KioskTypeID == kioskTypeID
                        select cc);
    return returnSet;
}

And this works just fine for returning the list of ContentChannels. But the Attachment in each ContentChannel is null.

I have tried [Include] on the attachment property in my ContentChannel metadata class, in conjuction with ContentChannels.Include("Attachment") in the above query- no luck, Attachment is always null.

I dug more and then found something to explicitly load my child item:

ObjectContext.LoadProperty( returnSet, "Attachment" );

But this generates the following error:

Cannot explicitly load property for entities that are detached. Objects loaded using the NoTracking merge option are always detached.

Is it because I'm doing a join that things are wonky and the Include doesnt work? What do I need to do? These attachments need to get loaded when I get a ContentChannel!

Any thoughts?

+1  A: 

By including the join in your query, you've changed the shape of the query, after the Include. So the Include is discarded. But you don't need the join:

public IQueryable<ContentChannel> GetContentChannelFromKioskType( long kioskTypeID )
{
    var returnSet = (from cc in ObjectContext.ContentChannels.Include( "Attachment" )
                     where cc.PublishedContentChannels.Any(pcc => pcc.KioskTypeID == kioskTypeID)
                     select cc);
    return returnSet;
}
Craig Stuntz
This doesnt compile. cc does have ContentChannel in it, it has PublishedContentChannels- and I cant access KioskTypeID in PCC. Nice article though!
Nicros
Well, I had to guess at the association/navigation name and multiplicity. I'll update the answer with the info you've added
Craig Stuntz
This worked: (from cc in ObjectContext.ContentChannels.Include( "Attachment" ) from pcc in cc.PublishedContentChannels.Where(e => e.KioskTypeID == kioskTypeID) select cc) But I still needed the Where statement and a from; you used a where then any. Is this correct as well?
Nicros
Correct? Maybe. It's different. Your version returns 1 row per PCC (so if a CC has 2 PCCs, you get 2 CC rows with the same CC). Mine returns 1 per CC. Pick whichever is right for you.
Craig Stuntz