Сущность
public class Category : Entity
{
public int Id { get; set; }
public string Name { get; set; }
public int? ParentId { get; set; }
public virtual Category ParentCategory { get; set; }
public virtual ICollection<Category> SubCategories { get; set; }
}
И отображение
public class CategoryMap : EntityTypeConfiguration<Category>
{
public CategoryMap()
{
HasKey(category => category.Id);
Property(category => category.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasOptional(category => category.ParentCategory)
.WithMany(category => category.SubCategories)
.HasForeignKey(category => category.ParentId);
}
}