7

这是我用来尝试包含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 语句给出了这个错误

包含路径表达式必须引用在类型上定义的导航属性。对引用导航属性使用虚线路径,对集合导航属性使用 Select 运算符。

有问题的模型

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; }
}

如何包含类型的User表?ProjectDocCover

更新:根据答案。我将模型更新为Cover看起来像这样,并删除了我所说的导致错误的包含。我现在可以获取数据:

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

1 回答 1

2

It is currently not supported. Eager loading relations on derived types doesn't work. The best you can do is to execute separate query to load all required Users for Covers already loaded by the first query and let EF do its magic (it should fill your navigation properties on already loaded entities but you must have lazy loading turned off).

于 2013-01-24T12:14:34.603 回答