在Visual Studio 2015中添加视图时发生错误

时间:2018-09-02 08:12:29

标签: asp.net-mvc visual-studio-2015

所以我在应用程序中使用这些类。

 public class Expenses
 {
    public Category Category { get; set; }
    public PaymentModes PaymentModes { get; set; }
 }

 public class Category 
 {
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }
 }

public class PaymentModes
 {
    public int PaymentModeID { get; set; }
    public string PaymentMode { get; set; }
 }

现在,当我在Details方法中添加视图时,它将引发以下错误。

 public ActionResult Details(int id)
 {
    return View();
 }

enter image description here

enter image description here

我该怎么办?

1 个答案:

答案 0 :(得分:1)

查看错误消息。明确地说,您没有为PaymentModesExpenses模型类定义主键。请编写您的所有三个模型类,如下所示:

public class Category 
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }
}

public class PaymentModes
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int PaymentModeID { get; set; }
    public string PaymentMode { get; set; }
}

public class Expenses
{
     [Key, ForeignKey("Category"),Column(Order = 0)]
     public int CategoryID { get; set; }

     [Key, ForeignKey("PaymentModes"),Column(Order = 1)]
     public int PaymentModeID { get; set; }

     public Category Category { get; set; }

     public PaymentModes PaymentModes { get; set; }

}