gpt4 book ai didi

c# - EF - 嵌套包含抛出错误

转载 作者:太空宇宙 更新时间:2023-11-03 19:47:34 26 4
gpt4 key购买 nike

我有一个跟踪学校项目的应用程序。项目有主题,而这些主题有任务。尽管一个项目和一个团队已经确定了领导者。学生应该轮流担任不同任务和科目的领导者(科目是我们所涵盖的不同书籍的部分)。我注意到在我的主题和任务的对象中,这些对象的领导者没有被加载。所以我试图在我的 Entity Framework 查询中添加一个我在网上看到的嵌套包含,以便它们被加载。但是,当我运行我的查询时,它给了我这个错误。任何人都可以看到我做错了什么并且知道如何解决这个问题吗?

            string name = data.value;
var project = await context.Projects
.Include(p => p.Subjects)
.ThenInclude(s => s.Tasks)
.Include(p => p.Subjects.Select(s => s.Leader))
.Include(p => p.Students)
//.ThenInclude(h => h)
.Include(p => p.Leader)
.Include(p => p.Team)
.ThenInclude(t => t.Leader)
.Include(p => p.Type)
.AsNoTracking()
.FirstOrDefaultAsync(p => p.Name == name);

项目.cs

public class Project
{
[Key]
public int ID { get; set; }

public int TypeID { get; set; }
public int? TeamID { get; set; }
public int? LeaderID { get; set; }

public string Name { get; set; }
public string Number { get; set; }
public string Details { get; set; }
public bool IsActive { get; set; }


[ForeignKey("TypeID")]
public virtual ProjectType Type { get; set; }
[ForeignKey("TeamID")]
public virtual Team Team { get; set; }
[ForeignKey("LeaderID")]
public virtual User Leader { get; set; }

public virtual ICollection<Subject> Subjects{ get; set; }
public virtual ICollection<User> Students { get; set; }


public Project()
{
Subjects= new List<Subject>();
Students = new List<User>();
}
}

主题.cs

public class Subject
{
[Key]
public int ID { get; set; }
//
public int? ProjectID { get; set; }
public int TypeID { get; set; }
public int? LeaderID{ get; set; }

public string Name { get; set; }
public string Details { get; set; }
public Decimal Estimate { get; set; }


[ForeignKey("ProjectID")]
public virtual Project Project { get; set; }
[ForeignKey("TypeID")]
public virtual SubjectType Type { get; set; }
[ForeignKey("LeaderID")]
public virtual User Leader { get; set; }

public virtual ICollection<Task> Tasks { get; set; }

public Subject()
{
Tasks = new List<Task>();
}

任务

  public class Task
{
[Key]
public int ID { get; set; }
public int? SubjectID { get; set; }
public int? TypeID { get; set; }
public int? LeaderID{ get; set; }

public string Name { get; set; }
public string Details { get; set; }
public Decimal Estimate { get; set; }

[ForeignKey("SubjectID")]
public virtual Subject Subject { get; set; }
[ForeignKey("TypeID")]
public virtual TaskType Type { get; set; }
[ForeignKey("LeaderID")]
public virtual User Leader { get; set; }
public Task() { }
}

错误

Message = "The property expression 'p => {from Subjects s in [p].Subjects select [s].Leader}' is not valid. The expression should represent a property access: 't => t.MyProperty'. For more information on including related data, see http://go.microsoft.com/fwl...

最佳答案

错误信息已经告诉你了。

您有 .Include(p => p.Subjects.Select(s => s.Leader)),这就是您在 EF6 中嵌套包含的方式。在 EF Core 中,您必须使用 .ThenInclude!

所以只需将 .Include(p => p.Subjects.Select(s => s.Leader)) 更改为 .Include(p => p.Subjects).ThenInclude( s => s.Leader))

关于c# - EF - 嵌套包含抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43527171/

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