Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 自引用实体不可为空_Entity Framework_Mapping_Code First - Fatal编程技术网

Entity framework 自引用实体不可为空

Entity framework 自引用实体不可为空,entity-framework,mapping,code-first,Entity Framework,Mapping,Code First,在可能的情况下,我想先使用这个Person类,它不是ef代码中名为RelatedPerson的可为null的属性: public class Person { public virtual int Id { get; set; } // ... other properties [Required] public virtual Person RelatedPerson { get; set; } } 现在,我如何添加映射来定义此关系,对于作为Pesron表根

在可能的情况下,我想先使用这个Person类,它不是ef代码中名为RelatedPerson的可为null的属性:

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

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

现在,我如何添加映射来定义此关系,对于作为Pesron表根的第一条记录,Id为1。

您不能向自引用属性添加
RequiredAttribute
。它不会验证没有RelatedPerson的根实体


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

您的最后一句话不清楚。你能试着用些别的词吗?