自引用实体不可为空

时间:2012-08-16 06:06:45

标签: entity-framework mapping code-first

在可能的情况下,我想要使用此Person类,并且它首先在ef代码中不是可称为RelatedPerson的属性:

public class Person
{
    public virtual int Id { get; set; }
    // ... other properties

    [Required]
    public virtual Person RelatedPerson { get; set; }
}

现在,我怎么能添加映射来定义这个关系,并且作为Pesron表的根的第一个记录,Id就是一个。

1 个答案:

答案 0 :(得分:2)

您无法将RequiredAttribute添加到自引用属性。它不会验证没有RelatedPerson的根实体。

您可以通过保存和更新功能中的业务逻辑强制执行它。

相关问题