0

更好地理解级联。有人可以解释为什么在节点 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();
4

1 回答 1

2

您的图表如下所示

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

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

如您所见,您没有从AC和从A到的链接D(仅在相反的方向上)

因此,当您保存ANHibernate 时,会尝试保存 的所有依赖项A,并找到 unsaved B,然后E然后F

于 2012-08-01T12:57:29.083 回答