Reputation: 65
So I am using these classes in my application.
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; }
}
Now when I add view in Details method it throws below error.
public ActionResult Details(int id)
{
return View();
}
What should I do?
Upvotes: 0
Views: 52
Reputation: 32139
Look at the error message. Its clearly saying that you don't primary key defined for the PaymentModes
and Expenses
model classes. Please write your all three model classes as follows:
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; }
}
Upvotes: 1