【问题标题】:Include Derived Models Related Class包括派生模型相关类
【发布时间】:2013-01-24 05:10:45
【问题描述】:

这是我用来尝试包含 User 表的 Lambda 表达式,它会引发错误。

ICollection<Activity> activity = db.Activities
            .Include(i => i.Project.ProjectDoc.OfType<Cover>().Select(v => v.User))
            .Where(u => u.UserID == WebSecurity.CurrentUserId)
            .OrderByDescending(d => d.DateCreated).ToList();

include 语句给出了这个错误

包含路径表达式必须引用定义在 方式。使用虚线路径作为参考导航属性和选择 集合导航属性的运算符。

有问题的模型

public abstract class ProjectDoc
{
    public int ProjectDocID { get; set; }
    public int ProjectID { get; set; }
    public string DocTitle { get; set; }
    public string Status { get; set; }
    public string Access { get; set; }
    public DateTime DateCreated { get; set; }


    public virtual ProjectDocAccess ProjectDocAccess { get; set; }
    public virtual Project Project { get; set; } 
    public virtual ICollection<Comment> Comment { get; set; }
    public ICollection<ProjectDocVote> ProjectDocVote { get; set; }
}
public class Segment : ProjectDoc
{
    public string Content { get; set; }
}
public class Cover : ProjectDoc
{
    public string CoverURL { get; set; }
    public int UserID { get; set; }
    public User User { get; set; }
}

如何为Cover 类型的ProjectDoc 包含User 表?

更新: 根据答案。我将Cover 的模型更新为如下所示,并删除了我所说的导致错误的包含。我现在可以获取数据了:

public class Cover : ProjectDoc
{
    public string CoverURL { get; set; }
    public int UserID { get; set; }
    public virtual User User { get; set; }
}

【问题讨论】:

    标签: c# linq entity-framework lambda


    【解决方案1】:

    目前是not supported。派生类型上的急切加载关系不起作用。您可以做的最好的事情是执行单独的查询来加载第一个查询已经加载的封面所需的所有用户,并让 EF 发挥它的魔力(它应该在已加载的实体上填充您的导航属性,但您必须关闭延迟加载)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      相关资源
      最近更新 更多