I am having trouble to map a tree structure using Fluent NHibernate, without also having to put in what I believe is a hack.
Consider the following classes:
Node
(abstract, hasint Id
andGroupNode Parent
(ifnull
, the node resides in the root node))GroupNode
(inherits fromNode
, hasIList<GroupNode> Groups
andIList<ItemNode> Items
)ItemNode
(inherits fromItem
)
Ideally, this would have the following database structure:
- GroupNodes (integer Id, nullable integer ParentId)
- ItemNodes (integer Id, nullable integer ParentId)
My mappers looks like this:
public class GroupNodeMap : ClassMap<GroupNode>
{
public GroupNode()
{
Id(x => x.Id);
References(x => x.Parent);
HasMany(x => x.Groups).LazyLoad();
HasMany(x => x.Items).LazyLoad();
}
}
public class ItemNodeMap : ClassMap<ItemNode>
{
public ItemNodeMap()
{
Id(x => x.Id);
References(x => x.Parent);
}
}
Unfortunately, this is creating a duplicate set of references (each table gets both a ParentId
and a GroupNodeId
. I can tweak this behaviour by adding .KeyColumn("ParentId")
after .LazyLoad()
, but this feels like a hack, and I would like it to be expressed using either conventions or lambdas instead of a magic string.
Could anyone point me in the right direction?