开发者

Loading a Child (Navigation Property) Entity not working

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?


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜