1

我有实体

/// <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 时添加组时。我得到了异常

无法确定相关操作的有效排序。由于外键约束、模型要求或存储生成的值,可能存在依赖关系。

4

1 回答 1

0

您正在使用此结构在数据库中创建一个自引用表。所以ParentGroupId必须是可以为空的。根节点不能有父节点,但是由它产生的所有节点都将有父节点,因此您需要将其设为ParentGroupId可空。

于 2013-01-03T11:47:01.850 回答