简体   繁体   English

指定的包含路径无效。 EntityType'SpiceShop.Models.Product'未声明名为'Products'的导航属性

[英]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: 但是,支持这种“父子关系”的更常见的使用场景是:

  • Your Category List invokes a query by ID when clicked, not a query by Name (ie 3, not "Pickles") 您的类别列表在单击时按ID调用查询,而不是按名称查询(即3,而不是“Pickles”)
  • Your View supports all the related Products, not just the first one (ie a model of type List<Product> , not just Product ) 您的View支持所有相关产品,而不仅仅是第一个产品(即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.

相关问题 指定的包含路径无效。 EntityType 未声明名称为 * 的导航属性 - A specified Include path is not valid. The EntityType does not declare a navigation property with the name * 指定的包含路径无效。 EntityType 'DB_A50B96_aljawdahlabModel.LAB_RESULTS' - A specified Include path is not valid. The EntityType 'DB_A50B96_aljawdahlabModel.LAB_RESULTS' Include 内部使用的 Lambda 表达式无效。 EF6,导航属性 - Lambda expression used inside Include is not valid. EF6, Navigation Property 指定的包含路径无效 - A specified Include path is not valid URI中指定的查询无效。 在类型上找不到名为“名称”的属性 - The query specified in the URI is not valid. Could not find a property named 'Name' on type 在这种情况下,为什么“指定的演员表无效”是没有意义的。 或者是吗? - It makes no sense why “Specified cast is not valid.” in this case. Or does it? 指定的架构无效。 错误:同名的多种类型 - Schema specified is not valid. Errors: Multiple types with the name 指定的演员表无效。 林克 - specified cast is not valid. linq 实体框架Include():指定的包含路径无效 - Entity Framework Include() : A specified Include path is not valid 使用包含时“指定的包含路径无效” - "A specified Include path is not valid” when using Include
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM