Include路径表达式必须引用在类型上定义的导航属性



我有两个模型,分别是"Page">《产品》产品的PageID指的是Page的ID

在我的产品索引视图中,我需要将页面列表作为下拉列表,为此我使用

public ViewResult Index()
{
var products = _db.Products.Include(p => p.Page);
return View(products.ToList());
}

但我只需要PageGroup属性值为"产品">的页面。为此,我使用了

public ViewResult Index()
{
var products = _db.Products.Include(p => p.Page.PageGroup
.Contains(PageGroup.Product.ToString()));
return View(products.ToList());
} 

它给出了如下错误:

Include路径表达式必须引用导航属性在类型上定义。使用虚线路径进行参考导航属性和用于集合导航的Select运算符属性。参数名称:路径

而不是

var products = _db.Products.Include(p => p.Page.PageGroup.Contains(PageGroup.Product.ToString()));

你想要这样的东西。

var products = _db.Products.Include(p => p.Page).Where(p => p.Page.PageGroup.Contains(PageGroup.Product.ToString());

您可能需要包含更多的子属性(如PageGroup)来检查您的实际情况,但如果不了解更多关于数据模型的信息,我不能确定。

最新更新