2009-09-14 42 views
3

我需要地圖具有枚舉的列表,以一個數據庫表,使用NHibernate地圖到Enums列表?

一類是這裏的對象

public class Driver : IIdentity 
{ 
    private IList<Licence> licences; 

    /// <summary> 
    /// The drivers licences 
    /// </summary> 
    public virtual IList<Licence> Licences 
    { 
     get 
     { 
      return this.licences; 
     } 
     set 
     { 
      this.licences = value; 
     } 
    } 
    ..... rest of the class .... 
} 

//the enum 
public enum Licence 
{ 
    FivePersonCar = 5, 
    SixPersonCar = 6 
} 

-------------- - 這裏是數據庫表

TABLE [dbo].[DriverLicence]( [DriverId] [int] NOT NULL, [Level] [int] NOT NULL)

TABLE [dbo].[Driver]( [DriverId] [int] NOT NULL, [Name] [varchar](150) NULL)

-------------這裏是我的d流利地圖河

public class DriverMap : ClassMap<Driver> 
{ 
    public DriverMap() 
    { 
     Id(x => x.Id).WithUnsavedValue(0).GeneratedBy.Identity(); 

     Map(x => x.Name); 

     HasManyToMany(x => x.Licences) 
      .WithTableName("DriverLicence") 
      .AsElement("Level").AsBag(); 


     HasManyToMany(x => x.InsuredToDrive) 
      .CollectionType<InsurancedList>() 
      .WithTableName("InsuredWith"); 
    } 

} 

-----這將生成以下文件HBM

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access=""> 
    <class name="Taxi.DomainObjects.Driver, Taxi.DomainObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Driver`" xmlns="urn:nhibernate-mapping-2.2"> 
    <id name="Id" type="Int32" unsaved-value="0" column="DriverID"> 
     <generator class="identity" /> 
    </id> 
    <property name="Name" type="String"> 
     <column name="Name" /> 
    </property> 
    <bag name="Licences" table="DriverLicence"> 
     <key column="DriverId" /> 
     <many-to-many column="LicenceId" class="Taxi.DomainObjects.Licence, Taxi.DomainObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
    </bag> 
    <bag name="InsuredToDrive" collection-type="Taxi.DomainObjects.Collections.InsurancedList, Taxi.DomainObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="InsuredWith"> 
     <key column="DriverId" /> 
     <many-to-many column="CarId" class="Taxi.DomainObjects.Car, Taxi.DomainObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
    </bag> 
    </class> 
</hibernate-mapping> 

這裏是我的錯誤

「從表DriverLicence的關聯是指未映射類: Taxi.DomainObjects.Licence「

有人知道我做錯了什麼?

回答

5

枚舉被NHibernate認爲是一種原始類型,因此您不應該使用many-to-many與類attribute進行映射。在.hbm而言,你需要這樣的事:

<bag name="Licences" table="DriverLicence"> 
    <key column="DriverId" /> 
    <element column="LicenceId" type="Taxi.DomainObjects.Licence, Taxi.DomainObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> 
</bag> 

雖然這樣HBM映射可以省略長type屬性。我的流利語法不是很好,所以我恐怕不能幫你。 This question可能會有所幫助。

+2

的hasMany(X => x.Licences) .WithTableName( 「DriverLicence」) .AsElement( 「等級」)。AsBag(); – dbones 2009-09-14 22:32:04

0

這正是我所要求的here。通過我自己的討論,我無法弄清楚如何使它工作,所以我最終不得不採取一種解決方法。

翻譯我做了什麼到你的代碼:

您的許可類名稱更改爲別的東西(在這種情況下,我會用LicenseCode),然後創建一個名爲許可的對象。這個對象,至少在我的情況下,有一個Id和一個名字。 id實際上是在我的Enum中分配的整數值。如果你願意,你可以用同樣的方法使用枚舉的名字,只需要將下面的代碼變爲名字而不是id。在課堂上,添加屬性如下:

public virtual LicenseCode LicenseCode 
{ 
    get 
    { 
     return (LicenseCode)LicenseId; 
    } 
} 

然後,創建一個表來匹配你要存儲該對象(id和name)的數據

然後,創建映射。這將是直接的,因爲您只是映射Id和名稱。

+0

謝謝您的回答,我回答了你的信息,並留下了一個很好的鏈接 – dbones 2009-09-14 22:38:37

0

你有沒有考慮在你的枚舉類中使用Flags屬性,而不是使用它們的列表?

+0

嗨我不知道你的意思,我想要一個驅動程序的許可證(枚舉)列表(這是一個演示應用程序)。國旗(許可證)是如何替代的?如果這比使用的實施方案更好,請隨時通知我。否則主要問題已經解決 – dbones 2009-09-15 22:12:22