Dot-Net

在實體框架中包含多個導航屬性的語法是什麼?

  • January 15, 2021

我正在查詢具有多個導航屬性的實體(申請人),需要在查詢的包含部分中包含兩個導航屬性(Worker 和 StatusType)。

嘗試將一個屬性 Worker 包含為**.include(“Worker”)這可行,但是當我使用.include(“Worker, StatusType”)**獲取兩個導航屬性時,查詢失敗並顯示消息“無效的包含路徑”。

在實體框架中包含多個導航屬性的語法是什麼?

採用

Include("Worker").Include("StatusType")
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)

引用自:https://stackoverflow.com/questions/5884290