在Entity Framework中包含多个导航属性的语法是什么?

时间:2011-05-04 13:38:44

标签: .net asp.net entity-framework entity

我正在查询具有多个导航属性的实体(申请人),需要在查询的包含部分中包含两个导航属性(Worker和StatusType)。

尝试将一个属性Worker包含为 .include(“Worker”),但是当我使用 .include(“Worker,StatusType”)来获取两者时导航属性查询失败,并显示消息“invalid include path”。

在Entity Framework中包含多个导航属性的语法是什么?

3 个答案:

答案 0 :(得分:17)

使用

Include("Worker").Include("StatusType")

答案 1 :(得分:5)

或者,如果它是您要包括的属性的子属性,请尝试

.Include("Worker.StatusType")

答案 2 :(得分: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)