我有实体
/// <summary>
/// The greoup.
/// </summary>
public class Group
{
#region Public Properties
/// <summary>
/// Gets or sets the group id.
/// </summary>
[Key]
public int GroupId { get; set; }
/// <summary>
/// Gets or sets the parent group id.
/// </summary>
public int ParentGroupId { get; set; }
/// <summary>
/// Gets or sets the active.
/// </summary>
public int Active { get; set; }
/// <summary>
/// Gets or sets the description.
/// </summary>
public string Description { get; set; }
/// <summary>
/// Gets or sets the group guid.
/// </summary>
public Guid GroupGuid { get; set; }
/// <summary>
/// Gets or sets the order weight.
/// </summary>
public int OrderWeight { get; set; }
/// <summary>
/// Gets or sets the parent group.
/// </summary>
[ForeignKey("ParentGroupId")]
public virtual Group ParentGroup { get; set; }
/// <summary>
/// Gets or sets the groups.
/// </summary>
public virtual ICollection<Group> Groups { get; set; }
#endregion
}
如何允许退出外键。因为当我尝试在ParentGroupId = 0时添加Group。我得到异常
无法确定相关操作的有效排序。由于外键约束,模型要求或存储生成的值,可能存在依赖关系。
答案 0 :(得分:0)
您正在使用此结构在数据库中创建自引用表。所以ParentGroupId
必须是可空的。根节点不能有父节点,但是它产生的所有节点都有父节点,所以你需要使ParentGroupId
可以为空。