0

我是 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:         }

任何帮助是极大的赞赏!

4

1 回答 1

0

我不是这方面的专家,但也许你的表应该命名为“Divisions”,我认为表名中应该使用复数形式。

于 2013-10-01T11:57:29.340 回答