in may case i want use this Person class and it’s not nullable property called RelatedPerson in ef code first:
public class Person
{
public virtual int Id { get; set; }
// ... other properties
[Required]
public virtual Person RelatedPerson { get; set; }
}
now, how could i add mapping to define this relation and for first record as root of Pesron table the Id is one.
You cannot add a
RequiredAttributeto a self-referencing property. It would not validate for the root entity which does not have a RelatedPerson.You can enforce it to be required via business logic in Save and Update functions.