【发布时间】:2012-10-11 11:30:03
【问题描述】:
有一个简单的域..
public abstract class UserComment
{
public string Text { get; set; }
}
public class BlogComment : UserComment
{
public Blog Blog { get; set; }
}
public class PhotoComment : UserComment
{
public Photo Photo { get; set; }
}
有没有办法查询所有类型为 UserComment 的实体并加载了属性博客和照片?
var comment = DbContext.Set<UserComment>()
.Include(x => x.Blog) // will not compile
.Include(x => x.Photo) // will not compile
.FirstOrDefault();
if (comment is PhotoComment )
{
string url = (comment as PhotoComment).Photo.Url;
}
if (comment is BlogComment)
{
var dateCreated = (comment as BlogComment).Blog.DateCreated;
}
谢谢!
【问题讨论】:
-
如果您的问题提供了有用的信息(更不用说点赞),您应该接受(标记为已接受)。这是对回答您问题的人的奖励。
-
这个链接在哪里?我看不到任何名为“接受答案”的按钮或链接。
-
我认为it's here
-
好的,现在,我想这不是代码优先?你试过
comment.Blog.Load()explanation -
我先使用代码。你提到的,我查看了加载扩展。并找到了可能的解决方案 - 将父类转换为子类并使用加载扩展:
context.Entry(comment as BlogComment).Reference(p => p.Blog).Load();
标签: c# entity-framework linq-to-entities ef-code-first