[英]With DevForce Ideablade, how do I determine if a related entity has been removed from an entity?
[英]How to determine if a related child-entity has been included
我有一個使用ASP.NET Core / EF Core構建的Web應用程序,帶有兩個實體“ Products”和“ ProductOptions”(一對多關系)。
當我執行查詢以從數據庫中檢索產品時,是否可以確定是否包含相關的ProductOptions(產品的子級)? 我需要區分以下情況:
情況1:查詢中包含產品的相關ProductOptions,但未找到ProductOptions
public Product GetByIdIncludingProductOptions(int id)
{
return DbContext.Products.Include(p => p.ProductOptions).FirstOrDefault(p => p.Id == id);
}
情況2:根本不包含產品的相關ProductOptions
public Product GetById(int id)
{
return DbContext.Products.FirstOrDefault(p => p.Id == id);
}
您可以使用ChangeTracker來檢查引用是否已加載。 您可能想要執行以下操作:
var isLoaded = dbContext.ChangeTracker.Entries<Products>()
.FirstOrDefault(ee => ee.Entity == product)
.Reference(e => e.ProductOptions)
.IsLoaded;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.