I am trying to describe a tree like structure where each item is a node in the tree and can (or not) have a parent.
When describing the Node entity, this works:
public class Node
{
public int Id {get;set;}
public string Description {get;set;}
public int ParentId {get;set;}
}
I get a table called Nodes with the above columns and a PK on Id. If I then add a new parameter:
public virtual Node Parent {get;set;}
for quick/easy access to the parent node, the creation code adds a foreign key constraint requiring ParentId to point to a Node Id.
Since some nodes will not have a parent, attempting to add a Node with a ParentId of 0 fails.
Is there a way to do this?
int?as data type forParentIdwhich means that the parent id is optional andnullwhen a node doesn't have a parent.