AJ_
AJ_

Reputation: 3987

EF - nested include throwing error

I have a application that tracks school projects. Projects have subjects and those subjects have tasks. Though a project and a team have set leaders. Students are meant to take turn being leaders of different tasks and subjects( Subjects are sections of the different books we are covering) . What i noticed was that inside my subject's and task's objects, the leaders of those objects were not getting loaded. So I'm trying to add a nested include, that i have seen online, in my entity framework query, so that they get loaded.However, when i run my query it gives me this error. Can anyone see what I'm doing wrong and know how to fix this?

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

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

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

Tasks

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

Error

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...

Upvotes: 1

Views: 693

Answers (1)

Tseng
Tseng

Reputation: 64297

The error messages tells you already.

You have .Include(p => p.Subjects.Select(s => s.Leader)), which is how you did nested includes in EF6. In EF Core you have to use .ThenInclude!

So just change .Include(p => p.Subjects.Select(s => s.Leader)) to .Include(p => p.Subjects).ThenInclude(s => s.Leader))

Upvotes: 5

Related Questions