EF - 嵌套包括抛出错误

时间:2017-04-20 18:26:39

标签: c# entity-framework linq asp.net-core entity-framework-core

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

constructor(public broadcastService: BroadcastService) {
    if(this.broadcastService.subject(Event.BLA_EVENT).observers.length == 0) {
        this.broadcastService.subject(Event.BLA_EVENT).subscribe(() => this.bla());
    }
}

Project.cs

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

Subjects.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>();
        }
    }

任务

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() { }
    }

1 个答案:

答案 0 :(得分:5)

错误消息已经告诉您。

你有.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))

即可