2010-12-02 38 views
1

我有我將表的列映射到同一個表的主鍵的情況。該表看起來像這樣在親子關係中映射表本身Nhibernate

---+-------+--------- 
ID Name ParentId 
---+-------+--------- 
1 Parent1 0 
2 Child 1 1 
3 Child 2 1 
4 Parent2 0 
5 Child 3 4 

我創建了以下型號和功能NHibernate映射類

//Model.LocationType.cs 
public class LocationType 
    { 
     public virtual long Id { get; set; } 
     public virtual string ShortName { get; set; } 
     public virtual string Description { get; set; }   
     public virtual IList<LocationType> ParentId { get; set; } 
    } 

//Mapping.LocationTypeMap.cs 
public class LocationTypeMap : ClassMap<LocationType> 
    { 
     public LocationTypeMap() 
     { 
      Table("SET_LOC_TYPE"); 
      Id(x => x.Id).Column("LOC_TYPE_ID").GeneratedBy.Assigned(); 
      Map(x => x.ShortName, "SHORT_NAME").Length(15).Not.Nullable(); 
      Map(x => x.Description, "LOC_DESC").Length(50).Not.Nullable(); 
      References(x => x.ParentId).Column("PARENT_LOC_TYPE_ID").Cascade.SaveUpdate(); 
     } 
    } 

,但我收到的時候我執行我的代碼如下錯誤信息:

Unable to cast object of type 'SmartHRMS.Core.Domain.Model.LocationType' to type 'System.Collections.Generic.IList`1[SmartHRMS.Core.Domain.Model.LocationType]'. 

編輯1: 而不是使用我試圖

HasMany(x => x.ParentIds).KeyColumn("PARENT_LOC_TYPE_ID"); 

雖然它的工作,並解決了我上面提到的鑄造的問題,但我流汗的結果是什麼,我需要的相反。 在父母的locationType對象,它列出了所有的IList孩子的,所以上面例子中的結果將是:

-----+----------+------ 
ID  Name  ParentId 
-----+----------+------ 
1  Parent1  IList<Child1, Child2> 
2  Child 2  IList<Empty> 
3 .... same 
4  Parent2  IList<Child3> 
5  Child 3  IList<Empty> 

回答

1

好像你應該在你的映射,而不是References使用HasMany

+0

我做過了,請查看我編輯過的帖子,瞭解它造成的問題 – Waqas 2010-12-02 11:50:20