2009-07-31 23 views
0

我已經得到了我不知道流利NHibernate的支持的數據模型 - 想知道是否有人能告訴我它是否確實,如果是的話,該怎麼辦呢。其基本結構是:NHibernate的「的hasMany」沒有明確的FKS

create table Container (
    id int identity(1,1) NOT NULL, 
    root_item_id int 
) 

create table ItemRelationship (
    id int identity(1, 1) NOT NULL, 
    parent_item_id INT, 
    child_item_id INT 
) 

create table Item (
    id int identity(1, 1) NOT NULL, 
    description VARCHAR(20) 
) 

所以一言以蔽之: 1)集裝箱有根項目 2)的項目,有小孩項目

我想在我的「容器」實體屬性,是它的根項目的孩子的項目的集合。我可以看到如何設置「直接」 FK的關係,但是這個人是有點不尋常,因爲關係鏈是:

Container.root_item_id - > ItemRelationship.parent_item_id

這裏沒有一個明確的FK那裏。我假設我必須以某種方式使用「Where」方法,但我不確定如何 - 無法找到示例。有任何想法嗎?

回答

0

我覺得你的問題可以用一個相對容易的方便特性來解決;讓我看看,如果我有這個權利:

你有很多:在項間關係的許多(保持在ItemRelationship表)。因此,默認情況下,您應該能夠爲ChildItems定義一個帶有其屬性的Item類(您的ClassMap將有一個HasManyToMany()調用)。您的容器類將具有Item類型的Root_Item屬性(正如剛剛提到的,它有一個ChildItems列表)。然後,您可以在Container上創建一個便利屬性,該屬性返回其Root_item的ChildItems。例如:

public class Container 
{ 
    public virtual Item Root_Item { get; set; } 

    public List<Item> ChildItems 
    { 
     get { 
      if (Root_Item != null) 
       return Root_Item.ChildItems; 
      return new List<Item>(); // or null depending on how you want to handle this 
     } 
    } 
} 
+0

這給了我我需要的東西!謝謝! – Marty 2009-07-31 17:39:56