0

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();
cs0815
  • 16,751
  • 45
  • 136
  • 299

1 Answers1

2

Your grapth looks like following

A <-> B <-> E <-> F

B <- C -> E
B <- D -> E

As you can see you have no links from A to C and from A to D (only in the opposite direction)

So, when you save A NHibernate is tring to save all dependencies of A, and finds unsaved B, then E and then F.

hazzik
  • 13,019
  • 9
  • 47
  • 86