映射到枚举列表?

dbo*_*nes 3 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
}
Run Code Online (Sandbox Code Playgroud)

----------------这里是DB表

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");
    }

}
Run Code Online (Sandbox Code Playgroud)

-----这会生成以下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>
Run Code Online (Sandbox Code Playgroud)

这是我的错误

"表DriverLicence中的关联引用了未映射的类:Taxi.DomainObjects.Licence"

有谁知道我做错了什么?

Joh*_*ner 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>
Run Code Online (Sandbox Code Playgroud)

虽然在这样的hbm映射中你可以省略long type属性.我的流利语法不是很好,所以我害怕在那里帮助你. 这个问题可能会有所帮助.

  • HasMany(x => x.Licences).WithTableName("DriverLicence").AsElement("Level").AsBag(); (2认同)