I'm trying to get a list of projects on the index page - but I keep on getting errors. I have tried different codes, and below is the latest one I've tried - but it's still not working. It gave me this error:
System.InvalidOperationException: A specified Include path is not valid. The EntityType 'StoreWeb.Models.Project' does not declare a navigation property with the name 'Collections'
I think it's because I don't know how to get collectionID from the Project table. For the Project table, it has collectionID is as a foreign key. (A project must have 1 collection. A collection may have projects.)
Can someone please help me? Here's what I have:
MODELS
Collection.cs [Modified]
[Table("Collection")]
public class Collection
{
[Key]
public string collectionID { get; set; }
public string collectionName { get; set; }
public List<Project> Projects { get; set; }
}
Project.cs [Modified]
[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; }
}
For the class Project, it has collectionID is as a foreign key. (A project must have 1 collection. A collection may have projects.)
ProductEntities.cs
public class ProductEntities : DbContext
{
public DbSet<Collection> Collections { get; set; }
public DbSet<Project> Projects { get; set; }
}
CONTROLLER
ProjectController [Modified]
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
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>
}
Just a typo
public ActionResult Index(string project)
{
var projectModel = productDB.Projects.Include("Collection");
return View(projectModel);
}
and not "Collections"
TIP (of the day)
in your usings, add
using System.Data.Entity;
and then you can make
var projectModel = productDB.Projects.Include(m => m.Collection);
you'll avoid "string" typos.
EDIT
for your classes, if you're using code First (which you are)
remove the "ForeignKeys" (collection_id in Project, for example), and just do
//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
}
in your project class
//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; }
}
NOTE: FOR EF CORE 3+ or 5+:
I know this has been answered but I faced similar issue using EF Core when reverse engineering or scaffolding a database:
This issue came up with include property on the indexes in my DbContext, when I have multiple properties involved in the Indexes.
After reverse engineering or Scaffolding from DB inside my OnModelCreating method I had this:
entity.HasIndex(e => e.BusinessName, "NonClusteredIndex-20190824-150740")
.IncludeProperties(new[] { "Contentid", "Stateid", "Cityid", "businessurl", "UserID" });
Then I changed it to this
entity.HasIndex(e => e.BusinessName, "NonClusteredIndex-20190824-150740")
.IncludeProperties(bmaster => new { bmaster.Contentid, bmaster.Stateid, bmaster.Cityid, bmaster.Businessurl, bmaster.UserId });
This resolved for me. In this case, EF Core couldn't use the string parameters, I hope this will be fixed in the new release of EF Core.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.