What is the syntax for including multiple navigation properties in Entity Framework?
Asked Answered
C

3

12

I am querying an Entity(Applicant) which has multiple navigation properties, need to include two navigation properties (Worker and StatusType) in the include part of the query.

Tried including one property Worker as .include("Worker") this works, but when I use .include("Worker, StatusType") to get both the navigation properties the query fails with the message 'invalid include path'.

What is the syntax for including multiple navigation properties in Entity Framework?

Carousel answered 4/5, 2011 at 13:38 Comment(1)
I would use the generic variants: .Include(a => a.Worker).Include(a => a.StatusType)Outsoar
K
18

Use

Include("Worker").Include("StatusType")
Kail answered 4/5, 2011 at 13:40 Comment(1)
You can also use Include(x => x.Worker) which is less error prone.Addams
W
6

Or if it is a subproperty of the property you are including try

.Include("Worker.StatusType")
Weigle answered 4/5, 2011 at 14:7 Comment(1)
You can also use Include(x => x.Worker.StatusType) which is less error prone.Addams
G
2
for example we have two class :

public class Address 
{
 [Required]
 public int ProvinceId { get; set; }

 [ForeignKey(nameof(ProvinceId))]
 public Province Province { get; set; }

}

public class Province 
{
 [StringLength(50)]
 [Required]
 public string Name { get; set; }
}

 //Now if you want to include province use code below : 

 .Include(x => x.Address).ThenInclude(x => x.Province)
Goodrich answered 15/1, 2021 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.