[英]A specified Include path is not valid. The EntityType 'SpiceShop.Models.Product' does not declare a navigation property with the name 'Products'
I Have Database that contains 2 tables 我有包含2个表的数据库
TABLE Categories (CategoriesId, Name, Description)
TABLE Products (ProductId, CategoriesId, Title, ProductImageUrl)
Categories is linked to Products by CategoriesId. 类别通过CategoriesId链接到Products。
I am trying to use LINQ to get all of a particular Title. 我正在尝试使用LINQ获取所有特定标题。
public ActionResult Browse(string categories)
{
var spices = spiceDB.Products.Include("Products").Single(p => p.Title == categories);
return View(spices);
}
Product Model 产品型号
namespace SpiceShop.Models
{
public class Product
{
[Key]
public int ProductId { get; set; }
public int CategoriesId { get; set; }
public string Title { get; set; }
public string ProductImageUrl { get; set; }
public List <Categorie> Name { get; set; }
}
}
Categorie Model 分类模型
namespace SpiceShop.Models
{
public class Categorie
{
[Key]
public int CategoriesId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
//public List<Product> ProductId { get; set; }
public List<Product> Products { get; set; }
}
}
Just remove the .Include("Products"), that's not how it's used. 只需删除.Include(“Products”),这不是它的使用方式。
The error is clear, there is no "Products" property at your "Product" model. 错误很明显,“产品”模型中没有“产品”属性。
Edit: 编辑:
It looks like your View requires a Model that is a single object of type "Product". 看起来您的View需要一个Model为“Product”类型的单个对象。
Also, I have tried to give each of my identifiers/variables a name that best conveys their meaning and intent. 此外,我试图给每个标识符/变量一个最能传达其含义和意图的名称。
Edit 2: 编辑2:
Please note that changing any input parameter names on a method will require a corresponding change in your View code that calls the method. 请注意,更改方法上的任何输入参数名称都需要在调用该方法的View代码中进行相应的更改。
In MVC, Action-Method parameters are automatically mapped . 在MVC中,Action-Method参数会自动映射 。
Your debug shows a URL of Browse?categories=Sauces
, which is automatically mapped to the method "Browse" with input parameter categories
set to "Sauces". 您的调试显示一个
Browse?categories=Sauces
的URL,它自动映射到方法“Browse”,输入参数categories
设置为“Sauces”。 But (my version of) the Browse method is expecting a categoryName
parameter, not categories
. 但是(我的版本)Browse方法需要
categoryName
参数,而不是categories
。 So you will need to make sure that URL Attribute and the Method Parameter have exactly the same name. 因此,您需要确保URL属性和方法参数具有完全相同的名称。
So if you really need to pass the selected Category's Name as the input parameter: 因此,如果您确实需要将所选类别的名称作为输入参数传递:
public ActionResult Browse(string categoryName)
{
// Gets the CategoryId for the given categoryName, or zero if not found.
var categoryId = spiceDB.Categories
.Where(c => c.Name == categoryName)
.Select(c => c.CategoriesId)
.FirstOrDefault();
// Gets the first Product (only) for the specified categoryId.
var firstProduct = category.Products
.FirstOrDefault(p => p.CategoriesId == categoryId);
return View(firstProduct);
}
However, a much more common usage scenario for supporting this type of "parent-child" relationship would be: 但是,支持这种“父子关系”的更常见的使用场景是:
List<Product>
, not just Product
) List<Product>
类型的模型,而不仅仅是Product
) eg 例如
public ActionResult Browse(int categoryId)
{
var products = spiceDB.Products
.Where(p => p.CategoriesId == categoryId)
.ToList();
return View(products);
}
ie making your Product List more comprehensive, and your data access code simpler and more efficient. 即使您的产品列表更加全面,您的数据访问代码更简单,更高效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.