![](/img/trans.png)
[英]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.