流畅的nhibernate级联图

时间:2012-08-01 09:53:27

标签: nhibernate fluent-nhibernate fluent-nhibernate-mapping

更好地了解级联。有人可以解释为什么在节点C和D下面的情况不会持续存在?感谢。

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;
    }
}

映射:

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(); 
    }
}

数据创建:

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();

1 个答案:

答案 0 :(得分:2)

你的格斗如下:

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

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

正如您所看到的,您没有从AC以及AD的链接(仅在相反方向)

因此,当您保存A时,NHibernate会保存A的所有依赖项,并找到未保存的B,然后是E,然后是F