这是我用来尝试包含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
表?ProjectDoc
Cover
更新:根据答案。我将模型更新为Cover
看起来像这样,并删除了我所说的导致错误的包含。我现在可以获取数据:
public class Cover : ProjectDoc
{
public string CoverURL { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
}