I am newbie with EF and trying to design my database using Code First. Following is my three Entities (I have not written all properties). I have an ode situation. The administrator is not allowed to edit an Expense, so I had to create another entity (EditedExpense) to have all edited expenses in another table. So when Administrator tries to change (edit) an expense and clicks save, a new post in EditedExpense will be created.
public class Expense
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ExpenseId { get; set; }
[Required]
public int CategoryId{ get; set; }
[ForeignKey("CategoryId")]
public virtual Category Category { get; set; }
public virtual List<EditedExpense> EditedExpenses { get; set; }
}
public class EditedExpense
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int EditedExpenseId { get; set; }
[Required]
public int CategoryId{ get; set; }
[ForeignKey("CategoryId")]
public virtual Category Category { get; set; }
public int ExpenseId { get; set; }
}
public class Category
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int CategoryId{ get; set; }
public string Title
public virtual List<Expense> Expenses { get; set; }
public virtual List<EditedExpense> EditedExpenses { get; set; }
}
It generates this error Introducing FOREIGN KEY constraint 'Expense_EditedExpenses' on table 'EditedExpense' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints