所以我在应用程序中使用这些类。
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();
}
我该怎么办?
答案 0 :(得分:1)
查看错误消息。明确地说,您没有为PaymentModes
和Expenses
模型类定义主键。请编写您的所有三个模型类,如下所示:
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; }
}