0

更好地理解級聯。有人可以解釋爲什麼在節點C和D下面的情況不會持續嗎?謝謝。流利的nhibernate級聯圖

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

我找不到要將哪個節點放入存儲庫 – hazzik 2012-08-01 12:22:26

+0

抱歉。編輯它。 – cs0815 2012-08-01 12:28:26

回答

2

你grapth看起來像下面

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

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

正如你可以看到你有沒有鏈接從ACAD(僅在相反方向)

所以,當你保存A NHibernate的是特林節省A所有的依賴關係,發現未保存B,然後E然後F

+0

我以爲是。我能做些什麼來實現我想要的,只需要我保存頂層節點(A)? – cs0815 2012-08-01 13:20:23

+0

不幸的是沒有。您需要'C'和'D'從'A'到達 – hazzik 2012-08-01 13:24:42