0

I want to build parent child entities for the same entity class in Entity Framework Core.

So I have a entity like this:

public class Definition
{
    public int Id{ get; set; }
    public int ParentId { get; set; }
    public string Name { get; set; }
    public bool Active { get; set; }
}

I want to set ParentId as foreign key that refers another Definition Entity as parent entity. How can I do this?

Thanks

Progman
  • 16,827
  • 6
  • 33
  • 48
tcetin
  • 979
  • 1
  • 21
  • 48
  • Does this answer your question? [Self referencing / parent-child relationship in Entity Framework](https://stackoverflow.com/questions/9955491/self-referencing-parent-child-relationship-in-entity-framework) – Progman Feb 01 '20 at 12:36

1 Answers1

0

add a foreign key attribute for parent defination

 public class Definition
 {  
    [Key]
    public int Id{ get; set; }

    public int? ParentId { get; set; }
    public string Name { get; set; }
    public bool Active { get; set; }

    [ForeignKey("ParentId")]
    public virtual Definition ParentDefinition { get; set; }
}
divyang4481
  • 1,584
  • 16
  • 32