我有模特:
public class Category
{
[Key]
public int CategoryId { get; set; }
public string Name { get; set; }
public int Parent { get; set; }
}
我想首先使用代码创建数据库,以及如何为Parent添加外键? (创建关系Parent-> CategoryId)
答案 0 :(得分:2)
public class Category
{
[Key]
public int CategoryId { get; set; }
public string Name { get; set; }
public int? ParentID { get; set; }
[ForeignKey("ParentID")]
public virtual List<Category> Subcategories { get; set; }
}