To understand cascade better. Can someone please explain why in the situation below node C and D are not persisted? Thanks.
public class Node : Entity
{
public virtual Node Previous { get; set; }
public virtual Node Next { get; set; }
public virtual string Name { get; set; }
public Node() { }
public Node(string Name)
{
this.Name = Name;
}
public virtual void Connect(Node Previous, Node Next)
{
this.Previous = Previous;
this.Next = Next;
}
}
Mapping:
public class NodeMap : IAutoMappingOverride<Node>
{
public void Override(AutoMapping<Node> mapping)
{
mapping.References(x => x.Previous).Cascade.SaveUpdate();
mapping.References(x => x.Next).Cascade.SaveUpdate();
}
}
Data creation:
INHibernateRepository<Node> NodeRepository = new NHibernateRepository<Node>();
Node A = new Node("A");
Node B = new Node("A");
Node C = new Node("C");
Node D = new Node("D");
Node E = new Node("E");
Node F = new Node("F");
A.Connect(null, B);
B.Connect(A, E);
C.Connect(B, E);
D.Connect(B, E);
E.Connect(B, F);
F.Connect(E, null);
NodeRepository.SaveOrUpdate(A);
NodeRepository.DbContext.CommitChanges();
Your grapth looks like following
As you can see you have no links from
AtoCand fromAtoD(only in the opposite direction)So, when you save
ANHibernate is tring to save all dependencies ofA, and finds unsavedB, thenEand thenF.