将映射到枚举列表?

3
我需要将一个包含枚举列表的类映射到数据库表中,使用NHibernate实现。
以下是对象:
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 map for Driver

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个回答

5

枚举类型在NHibernate中被视为原始类型,因此您不应该使用属性映射many-to-many。 在.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属性。但是,我的Fluent语法不是很好,所以恐怕无法帮助你。 这个问题可能会进一步帮助。


2
HasMany(x => x.Licences) .WithTableName("DriverLicence") .AsElement("Level").AsBag(); - dbones

0

这正是我在这里所问的。通过自己的尝试,我无法弄清楚如何使它工作,所以最终不得不采用一种变通方法。

为了将我所做的内容翻译成您的代码:

将您的许可证类名更改为其他名称(在本例中,我将使用LicenseCode),然后创建一个名为License的对象。在我的情况下,此对象具有Id和名称。 Id实际上是分配给我的枚举的整数值。如果您愿意,您可以以相同的方式使用枚举的名称,并只需更改下面的代码以便根据名称进行翻译而不是id。在类中,添加以下属性:

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

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

然后,创建映射。这会很简单,因为您只需要映射Id和名称。


谢谢您的回答,我已经在您的帖子中回复并留下了一个好链接。 - dbones

0
你有没有考虑过在枚举类中使用Flags属性,而不是将它们列成列表?

嗨,我不确定你的意思,我想要一个驾驶员许可证(这是一个演示应用程序)的许可证列表(枚举)。如果在枚举(许可证)上使用标志属性是否是一种替代方法?如果这比我正在使用的实现场景更好,请随时告诉我。否则,主要问题已解决。 - dbones

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接