映射到枚举列表?

时间:2009-09-14 21:05:23

标签: c# nhibernate orm fluent-nhibernate

我需要使用NHibernate

将具有Enums列表的类映射到db表

这是对象

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)

-------------这是我的Fluent驱动程序地图

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”

谁知道我做错了什么?

3 个答案:

答案 0 :(得分: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映射中你可以省略long type属性。我的流利语法不是很好,所以我害怕在那里帮助你。 This question可能会有所帮助。

答案 1 :(得分:0)

这正是我问的here。通过我自己的玩弄,我无法弄清楚如何使它工作,所以我最终不得不做一些解决方法。

将我所做的工作翻译成您的代码:

将您的许可证类名更改为其他名称(在本例中我将使用LicenseCode),然后创建一个名为License的对象。在我的情况下,这个对象至少有一个Id和一个名字。 id实际上是我的Enum中分配的整数值。如果需要,您可以以相同的方式使用枚举的名称,只需查看下面的代码即可翻译名称而不是ID。在课堂上,添加如下属性:

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

然后,创建一个表以匹配您将为此对象存储的数据(id和名称)

然后,创建映射。这将是直截了当的,因为您只映射了Id和名称。

答案 2 :(得分:0)

您是否考虑过将Flags属性与枚举类一起使用而不是列出它们?

相关问题