【问题标题】:System.InvalidOperationException: A specified Include path is not validSystem.InvalidOperationException:指定的包含路径无效
【发布时间】:2020-09-27 08:19:07
【问题描述】:

我正在尝试在索引页面上获取项目列表 - 但我不断收到错误消息。我尝试了不同的代码,下面是我尝试过的最新代码 - 但它仍然无法正常工作。它给了我这个错误:

System.InvalidOperationException:指定的包含路径无效。 EntityType 'StoreWeb.Models.Project' 没有声明名为 'Collections' 的导航属性

我认为这是因为我不知道如何从 Project 表中获取 collectionID。对于 Project 表,它有 collectionID 作为外键。 (一个项目必须有 1 个集合。一个集合可能有项目。)

有人可以帮帮我吗?这是我所拥有的:

模型


Collection.cs [修改]


[Table("Collection")]
public class Collection
{
    [Key]
    public string collectionID { get; set; }
    public string collectionName { get; set; }

    public List<Project> Projects { get; set; }
}

Project.cs [修改]


[Table("Project")]
public class Project
{
    [Key]
    public string projectID { get; set; }
    public string projectName { get; set; }
    public string projectCity { get; set; }
    public string collectionID { get; set; } // This is what was needed!

    public virtual Collection Collection { get; set; }
}

对于项目类,它有collectionID 作为外键。 (一个项目必须有 1 个集合。一个集合可能有项目。)


ProductEntities.cs


public class ProductEntities : DbContext
{
    public DbSet<Collection> Collections { get; set; }
    public DbSet<Project>    Projects  { get; set; }
}

控制器


ProjectController [修改]


using System.Data.Entity;

public class ProjectController : Controller
{
    ProductEntities productDB = new ProductEntities();

    //
    // GET: /Project/

    public ActionResult Index()
    {
        var projectModel = productDB.Projects.Include(m => m.Collection);
        return View(projectModel);
    }

观看次数


Views/Project/Index.chtml


@model IEnumerable<StoreWeb.Models.Project>

@{
     ViewBag.Title = "Index";
}
<h1>PROJECTS</h1>

     @foreach (var project in Model)
     {
         <br /><a href="@Url.Action("Details", new { id = project.projectID })">@project.projectName</a> 
     }

【问题讨论】:

    标签: c# asp.net-mvc model-view-controller


    【解决方案1】:

    只是错别字

    public ActionResult Index(string project)
        {
            var projectModel = productDB.Projects.Include("Collection");
            return View(projectModel);
        }
    

    而不是“收藏”

    提示(当天)

    在你的使用中,添加

    using System.Data.Entity;
    

    然后你就可以制作了

    var projectModel = productDB.Projects.Include(m => m.Collection);
    

    您将避免“字符串”拼写错误。

    编辑

    对于您的课程,如果您使用的是 code First(您是)

    删除“ForeignKeys”(例如项目中的collection_id),然后就可以了

    //remove the "Bind Exclude"
    public class Collection
    {
        public int Id { get; set; }//make it int, it will be taken as the primary key
        public string collectionName { get; set; }
    
        public virtual IList<Project> Projects { get; set; }//make it virtual, lazy loading inside
    }
    

    在你的项目类中

    //remove any "collection_Id" property
    public class Project
    {
        public int Id { get; set; }//make it it, rename it, it will be taken as PK
        public string projectName { get; set; }
        public string projectCity { get; set; }
    
        public virtual Collection Collection { get; set; }
    }
    

    【讨论】:

    • 感谢您的帮助,拉斐尔。我已经尝试了你的两个建议,但是他们都给了我这个错误: ::: System.Data.SqlClient.SqlException: Invalid column name 'Collection_collectionID' ::: (有一个列名“collectionID”项目和集合表。)还有什么我可以尝试的吗?
    • 有没有办法让我使用字符串作为 PK 的数据类型(b/c Project 和 Collection 的 PK 混合了字母和数字)?
    • 是的,只需将 [Key] 属性放在表定义中的 Id 上(在我的示例中):我认为(可能是错误的),只有当它是 int 时,EF 才会自动识别 Id 或 &lt;TableName&gt;Id
    • 例如,你的意思是这样的吗?::: [Key] public string projectID { get;放; } ::: 如果是这样,我已经尝试编辑两个表,但它仍然给了我同样的错误: System.Data.SqlClient.SqlException: Invalid column name 'Collection_collectionID' [Add't info -- 在错误页面上,它说源错误是:@foreach(模型中的var项目)]所以,也许这就是我需要改变的?但我知道怎么...
    • 您能否编辑您的第一篇文章并显示您的 2 个课程已完成和“修改”?
    【解决方案2】:

    注意:对于 EF CORE 3+ 或 5+:

    我知道这个问题已经得到解答,但我在逆向工程或构建数据库时遇到了类似的问题:

    当我在索引中涉及多个属性时,这个问题出现在我的 DbContext 中的索引上包含属性。

    在我的 OnModelCreating 方法中从 DB 进行逆向工程或脚手架后,我得到了这个:

    entity.HasIndex(e => e.BusinessName, "NonClusteredIndex-20190824-150740")
                     .IncludeProperties(new[] { "Contentid", "Stateid", "Cityid", "businessurl", "UserID" });
    

    那我改成这个了

    entity.HasIndex(e => e.BusinessName, "NonClusteredIndex-20190824-150740")
                     .IncludeProperties(bmaster => new { bmaster.Contentid, bmaster.Stateid, bmaster.Cityid, bmaster.Businessurl, bmaster.UserId });
    

    这为我解决了。在这种情况下,EF Core 无法使用字符串参数,希望在新版本的 EF Core 中修复。

    【讨论】:

      猜你喜欢
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多