Many to Many Mapping on a single class
Hi Guys
I have come across a situation.
I have two tables Node and NodeValidParents as follows
Node
NodeId_PK
Name
NodeValidParents NodeId_FK (Foreign key from Node)
ValidParentNodeId_FK (Foreign key from Node)
A Node can have a list of ValidParents. As you can see from the tables, a ValidParent is also a node itself. So there is actually a many to many relationship ( A node can have many ValidParents and a ValidParent can belong to many nodes)
My Node class looks like this
public class Node
{
public virtual int Id
{
get;
set;
}
public virtual string Name
{
get;
set;
}
public virtual IList<Node> ValidParents
{
get;
protected set;
}
public Node()
{
ValidParents = new List<Node>();
}
}
I don't have any class to represent ValidParent because I think there is no need for it, as ValidParent is itself a Node.
I am using autoMapping and I just can't figure out how to map this class to these two tables. I am implementing IAutoMappingOverride for overriding automappings for Node but I am not sure what to do in it.
public class NodeMap : IAutoMappingOverride
{
public void Override(AutoMapping<Node> mapping)
{
//mapping.HasMany(x => x.ValidParents).Cascade.All().Table("NodeValidParents");
}
}
Could anybody out there help me with this please?
Also I want to make sure that the combination of NodeId_FK and ValidParentNodeId_FK is always unique or Composite Key. I have seem people saying using composite keys is not the best practice and surrogate key should be used. I have no problem with using Surrogate key but I want to make sure that if a node already has a ValidParent , it can not be inserted again. How can i achieve this with fluent nhibernate mapping?
Just so that you know I have never used NHibernate with XML mapping. I have only used Fluent NHibernate.
Thanks
Nabeel