Juanker
Juanker

Reputation: 794

Foreign key to the same table

I'm trying users can comment publications. I've created the table Comments with EF Core.

There are multiple levels of comments and I've implmeneted the following way:

Table Publications

- Id (primary key)

Table Comments

- PublicationId (foreing key to Publication)
- Comment
- ParentComment (foreign key to Comments, that is, the same table)

The problem is an error warn me I can't créate a foreing key to the same table. How can I implement the hierarchy then?

Upvotes: 1

Views: 610

Answers (1)

Master
Master

Reputation: 2163

Your code is suppose to look something like this

public int Id {get; set;}
public virtual Publication Publication {get; set;} // If Publication is an object
public virtual Comment Comment {get; set;}
public virtual Comment ParentComment {get; set;}

Upvotes: 1

Related Questions