インデックスページでプロジェクトのリストを取得しようとしていますが、エラーが発生し続けます。私はさまざまなコードを試しましたが、以下は私が試した最新のコードですが、それでも機能しません。それは私にこのエラーを与えました:
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; }
}
クラスProjectの場合、外部キーとして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>
}