gpt4 book ai didi

c# - 包括派生模型相关类

转载 作者:太空狗 更新时间:2023-10-29 20:36:58 25 4
gpt4 key购买 nike

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

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.

有问题的模型

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

最佳答案

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

关于c# - 包括派生模型相关类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14494406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com