实体框架.加载子类属性的Quering父实体

Jek*_*kas 3 c# linq-to-entities entity-framework ef-code-first

有一个简单的域名..

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; }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法查询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;
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

Sla*_*uma 5

您可能需要两个查询才能获得结果.如果你只想要第一个元素(FirstOrDefault)显式加载 - 正如你在评论中已经提出的那样 - 是一个很好的方法:

var comment = DbContext.Set<UserComment>().FirstOrDefault();

if (comment is BlogComment)
    DbContext.Entry(comment as BlogComment).Reference(bc => bc.Blog).Load();
else if (comment is PhotoComment)
    DbContext.Entry(comment as PhotoComment).Reference(pc => pc.Photo).Load();
Run Code Online (Sandbox Code Playgroud)

如果你想加载一个UserComment不是最好的解决方案的列表,因为它需要迭代加载的UserComments并为每个元素调用显式加载,这将导致许多查询.

对于列表,您可以使用以下方法,该方法也只生成两个查询:

IEnumerable<UserComment> blogComments = DbContext.Set<UserComment>()
    .OfType<BlogComment>()
    .Include(bc => bc.Blog)
    .Cast<UserComment>()
    .AsEnumerable();

IEnumerable<UserComment> photoComments = DbContext.Set<UserComment>()
    .OfType<PhotoComment>()
    .Include(pc => pc.Photo)
    .Cast<UserComment>()
    .AsEnumerable();

List<UserComment> comments = blogComments.Concat(photoComments).ToList();
Run Code Online (Sandbox Code Playgroud)

由于使用AsEnumerable()它将运行两个单独的数据库查询并将结果连接到内存中的单个集合.

LINQ-to-Entities支持Cast但由于某种原因,无法删除这两个AsEnumerable()转换以仅获取单个数据库查询并将结果连接到数据库中.代码仍然可以编译,但我有一个关于无效Include路径的运行时异常.

我已经使用EF 4.1进行了测试.在不AsEnumerable()使用EF 5.0的情况下测试查询可能值得查看它是否仍然失败.