I have this situation:
public abstract class Node : Entity
{
public virtual Node Parent { get; set; }
private ICollection<Node> _children = new HashSet<Node>();
public virtual ICollection<Node> Children
{
get { return _children; }
set { _children = value; }
}
}
A node can have a maximum of one parent and several children. I use this mapping file:
public class NodeMap : IAutoMappingOverride<Node>
{
public void Override(AutoMapping<Node> mapping)
{
mapping.HasMany<Node>(x => x.Children)
.KeyColumn("ParentId")
.KeyNullable()
.AsSet()
.Inverse()
.Cascade.SaveUpdate()
.ForeignKeyConstraintName("FK_Node_ParentId")
.Not.LazyLoad();
}
}
The connection between nodes is only persisted if I add the parent to the child and the child to the parent. However, I would like to persist 'connections' between nodes if the parent is just added to the child. Can I achieve this with fnh mapping?
PS:
Let me try to specify things a bit clearer. I have 5 nodes (1,2,3,4,5).
There are bi directional connections between these:
Parent <-> Child
1 <-> 2
2 <-> 3
2 <-> 6
3 <-> 4
4 <-> 5
But only a unidirectional connection between 6 and 4, as 4 already has a parent namely 3.
Unfortunately, the connection between 6 and 4 is not persisted. Maybe I have to change the domain model but I was hoping to be able to achieve this ‘mixture of uni/bi directionality’.
Thanks.
PPS:
Saving:
INHibernateRepository<Node> NodeRepository = new NHibernateRepository<Node>();
...
NodeRepository.SaveOrUpdate(Node1); // save topnode
NodeRepository.DbContext.CommitChanges();