无法让 EF4 预加载子类实体正常工作
我的 EF4 模型中有一个抽象的 Content 实体,它有一个具体的子类 MultipleChoiceItem。在 MultipleChoiceItem 的情况下,有一个相关表,可通过 MultipleChoiceItem 实体上的导航属性访问,称为“选项”。我想立即加载选项结果,因为如果您获得 MultipleChoiceItem,您总是需要选项。但是,我似乎无法让它发挥作用。这是我基本上所做的:
MultipleChoiceItem 是一个子类,因此它没有默认创建的元数据。因此,我为其创建了一个元数据类,如下所示:
[MetadataTypeAttribute(typeof(MultipleChoiceItem.ContentMetadata))] 公共部分类 MultipleChoiceItem { 内部密封类 MultipleChoiceItemMetadata { ...其他东西... [包括] 公共EntityCollection
选项{获取;放; } ...其他东西... } 然后我在 ContentService 中创建了一个新查询,如下所示:
public IQueryable
;获取多项选择项() { 返回 this.ObjectContext.Contents.OfType () .include("选项"); }
在单元测试中,我这样调用它:
context = new ContentContext();
var loadOperation = context.Load(context.GetMultipleChoiceItemsQuery());
loadOperation.Completed += new EventHandler(CompletedHandler);
一旦进入 CompletedHandler,以下代码似乎不会加载了Options属性——事实上,Options.Count == 0,而它应该是4(通过检查底层数据存储来验证)。
MultipleChoiceItem mci = context.Contents.First(c => c.ContentId == mciId) as MultipleChoiceItem;
谁能告诉我我做错了什么?我试图遵循我看到的发布的模型,但显然遗漏了一些东西......
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我必须手动完成的元数据的元数据类型属性(因为出于某种原因,RIA 默认情况下不会为子类创建元数据)的类型错误。它无意中读到:
并且应该读到:
没有为子类生成元数据的事实是相当烦人的。
无论如何,在这里发帖以防其他人遇到这个问题!
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:
And should have read:
The fact that there is no metadata generated for subclasses is quite annoying.
Anyhow, posting here in case anyone else has this issue!