【问题标题】:What is the syntax for including multiple navigation properties in Entity Framework?在实体框架中包含多个导航属性的语法是什么?
【发布时间】:2011-05-04 13:38:43
【问题描述】:

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

尝试包含一个属性 Worker 作为 .include("Worker") 这可行,但是当我使用 .include("Worker, StatusType") 来同时获得导航属性 查询失败并显示消息“无效的包含路径”。

在实体框架中包含多个导航属性的语法是什么?

【问题讨论】:

  • 我会使用通用变体:.Include(a => a.Worker).Include(a => a.StatusType)

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


【解决方案1】:

使用

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

【讨论】:

  • 您也可以使用不易出错的Include(x => x.Worker)
【解决方案2】:

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

.Include("Worker.StatusType")

【讨论】:

  • 您也可以使用不易出错的Include(x => x.Worker.StatusType)
【解决方案3】:
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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 2013-07-05
    相关资源
    最近更新 更多