[Table("Menus")]
public class Menus
{
/// <summary>
/// Table "Menus", ID
/// </summary>
private int _ID;
[Key]
[Display(Name = "ID")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID
{
get { return _ID; }
set { _ID = value; }
}
/// <summary>
/// Table "Menus", MenuName
/// </summary>
private string _MenuName;
[Display(Name = "MenuName")]
public string MenuName
{
get { return _MenuName; }
set { _MenuName = value; }
}
/// <summary>
/// Table "Menus", ParentID
/// </summary>
private int _ParentID;
[Display(Name = "ParentID")]
public int ParentID
{
get { return _ParentID; }
set { _ParentID = value; }
}
}
我有这张桌子。我希望在ParentID上有“一对多”。我想从列表中选择父ID,并希望通过Entity框架或流畅的api来做,但不知道它们是好的。
答案 0 :(得分:1)
你会想做这样的事情:
[Table("Menus")]
public class Menus
{
[Key]
public int ID { get; set; }
public string MenuName { get; set; }
[ForeignKey("ParentMenu")]
public int ParentID { get; set; }
public virtual Menu ParentMenu { get; set; }
public virtual List<Menu> ChildMenus { get; set; }
}
答案 1 :(得分:0)
一切正常。谢谢。 我删除表中的所有行,然后创建迁移。但该财产必须可以为空:
public int? ParentID { get; set; }