C# System.InvalidOperationException:指定的包含路径无效
我试图在索引页上获得一个项目列表,但我不断出错。我尝试过不同的代码,下面是我尝试过的最新代码,但仍然不起作用。它给了我这个错误: System.InvalidOperationException:指定的包含路径无效。EntityType“StoreWeb.Models.Project”未声明名为“Collections”的导航属性 我想这是因为我不知道如何从项目表中获取collectionID。对于项目表,它将collectionID作为外键。(项目必须有1个集合。集合可以有项目。) 有人能帮我吗?以下是我所拥有的: 型号C# System.InvalidOperationException:指定的包含路径无效,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,我试图在索引页上获得一个项目列表,但我不断出错。我尝试过不同的代码,下面是我尝试过的最新代码,但仍然不起作用。它给了我这个错误: System.InvalidOperationException:指定的包含路径无效。EntityType“StoreWeb.Models.Project”未声明名为“Collections”的导航属性 我想这是因为我不知道如何从项目表中获取collectionID。对于项目表,它将collectionID作为外键。(项目必须有1个集合。集合可以有项目。) 有人能帮我
Collection.cs[已修改]
对于类项目,它将collectionID作为外键。(项目必须有1个集合。集合可以有项目。)
ProductEntities.cs
视图
视图/Project/Index.chtml
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
项目
@foreach(模型中的var项目)
{
}
只是一个打字错误
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);
您将避免“字符串”输入错误
编辑
对于您的类,如果您首先使用代码(您是)
删除“ForeignKeys”(例如,项目中的集合\u 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
}
注意:对于EF CORE 3+或5+:
entity.HasIndex(e => e.BusinessName, "NonClusteredIndex-20190824-150740")
.IncludeProperties(new[] { "Contentid", "Stateid", "Cityid", "businessurl", "UserID" });
我知道这个问题已经得到了回答,但当逆向工程或构建数据库时,我在使用EF Core时遇到了类似的问题:
当索引中涉及多个属性时,这个问题在我的DbContext中的索引上出现了include属性
在我的onmodel创建方法中,从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的新版本中得到修复。感谢您的帮助,Raphaël。我尝试了你的两个建议,但是他们都给了我这个错误::::System.Data.SqlClient.SqlException:无效的列名'Collection_collectionID':::(在项目和集合表中都有一个列名“collectionID”)。还有什么我可以尝试的吗?我有没有办法使用字符串作为PK的数据类型(b/c Project和Collection的PKs都有字母和数字的混合体)?是的,只需在表定义中的Id上添加
[Key]
属性(在我的示例中):我认为(可能是错误的),EF会自动识别Id或Id
,如果它是int。对于ex,您的意思是这样的吗?:[Key]公共字符串projectID{get;set;}:::如果是这样,我尝试编辑了这两个表,但它仍然给了我相同的错误:System.Data.SqlClient.SqlException:Collection\u collectionID'无效列名[Add't info--在错误页上,它说源错误是:@foreach(模型中的var项目)]所以,也许这就是我需要改变的?但我知道如何…你能编辑你的第一篇文章,并显示你的两个类完整和“修改”?
var projectModel = productDB.Projects.Include(m => m.Collection);
//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; }
}
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 });