Reputation: 447
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?
Upvotes: 0
Views: 352
Reputation: 397
do you have any restriction in your data model? if not just try to make parentId nullable as this:
public int? ParentId {get;set;}
Upvotes: 2