0
我是ASP.net MVC的新手,這是我的第一次嘗試。我使用關係表設計一個小型站點。 (保持它短,我刪除了一些不相關的領域)ASP.net MVC表關係
Items
:
Id int (PK)
description nvarchar(50)
divisionid nchar(2) FK
categoryid nchar(6) FK
Division
code nchar(2) PK
description nvarchar(50)
Category
code nchar(6) PK
description nvarchar(50)
我相信是相當自我解釋。試圖創建下面的型號爲它的時候:
public class Item
{
[Key]
[Display(Name="Id")]
public int Id { get; set; }
[Required(ErrorMessage="Description is required")]
[Display(Name = "Description")]
[StringLength(50, ErrorMessage = "Description must not be more than 50 characters long")]
public string Description { get; set; }
[Display(Name = "Division")]
[StringLength(2)]
public string DivisionId { get; set; }
[Display(Name = "Category")]
[StringLength(6)]
[ForeignKey("DivisionId")]
public virtual Division division { get; set; }
[ForeignKey("CategoryId")]
public virtual Category Category { get; set; }
}
public class Division
{
[Key]
[Display(Name = "Code")]
[StringLength(2, ErrorMessage = "Code must be 2 char.")]
public string Code { get; set; }
[Display(Name = "Description")]
[Required(ErrorMessage = "Description is required")]
[StringLength(50,ErrorMessage="Description must not be more than 50 characters long")]
public string Description { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
public class Category
{
[Key]
[Display(Name = "Code")]
[StringLength(6, ErrorMessage = "Code must be 6 char.")]
public string Code { get; set; }
[Display(Name = "Description")]
[Required(ErrorMessage = "Description is required")]
[StringLength(50, ErrorMessage = "Description must not be more than 50 characters long")]
public string Description { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
問題正在運行的網站的時候,我得到的異常無效的對象名稱dbo.Divisions
與控制器的返回值指向一個錯誤
Line 20: {
Line 21: var items = db.Items.Include(i => i.division).Include(i => i.Category);
Line 22: return View(items.ToList());
Line 23: }
任何幫助非常感謝!