Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 423
inlokesh Avatar asked May 04 '11 13:05

inlokesh


People also ask

What is navigation property in entity data model?

A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data.

What is navigation property in Linq?

Navigation properties allow a user to navigate from one entity to another, or from one entity to related entities through an association set. This topic provides examples in query expression syntax of how to navigate relationships through navigation properties in LINQ to Entities queries.

What is reference navigation property?

Reference navigation property: A navigation property that holds a reference to a single related entity. Inverse navigation property: When discussing a particular navigation property, this term refers to the navigation property on the other end of the relationship.


1 Answers

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)
like image 84
Omid Soleiman Avatar answered Oct 10 '22 03:10

Omid Soleiman