I have an abstract Content entity in my EF4 model with a concrete subclass, MultipleChoiceItem. There is a related table in the case of MultipleChoiceItem accessed by a Navigation property on the MultipleChoiceItem entity called Options. I would like to eager-load the Options result because if you're getting a MultipleChoiceItem, you always want the Options. However, I can't seem to get this to work. Here is what I've bascially done:
The MultipleChoiceItem is a subclass, therefore it didn't get metadata created by default. So, I created a metadata class for it that looks sort of like this:
[MetadataTypeAttribute(typeof(MultipleChoiceItem.ContentMetadata))]
public partial class MultipleChoiceItem
{
internal sealed class MultipleChoiceItemMetadata
{
... other stuff ...
[Include]
public EntityCollection<McOption> Options { get; set; }
... other stuff ...
}
I then created a new query in the ContentService that looks like this:
public IQueryable<Content> GetMultipleChoiceItems()
{
return this.ObjectContext.Contents.OfType<MultipleChoiceItem>()
.Include("Options");
}
In a unit test, I'm calling that like this:
context = new ContentContext();
var loadOperation = context.Load(context.GetMultipleChoiceItemsQuery());
loadOperation.Completed += new EventHandler(CompletedHandler);
Once I get into the CompletedHandler, the following code doesn't seem to have the Options property loaded--in fact, Options.Count == 0 when it should be 4 (verified by checking the underlying data store).
MultipleChoiceItem mci = context.Contents.First(c => c.ContentId == mciId) as MultipleChoiceItem;
Can anyone tell me what I'm doing wrong? I've tried to follow the models I've seen posted and am apparently missing something...
Thanks!
My metadata type attribute for the metadata that I had to do by hand (since RIA doesn't make metadata for subclasses by default for some reason) had the wrong type. It inadvertantly read:
[MetadataTypeAttribute(typeof(MultipleChoiceItem.ContentMetadata))]
And should have read:
[MetadataTypeAttribute(typeof(MultipleChoiceItem.MultipleChoiceMetadata))]
The fact that there is no metadata generated for subclasses is quite annoying.
Anyhow, posting here in case anyone else has this issue!