映射到枚举列表?

我需要使用NHibernate将具有Enums列表的类映射到db表

这是对象

public class Driver : IIdentity { private IList licences; ///  /// The drivers licences ///  public virtual IList Licences { get { return this.licences; } set { this.licences = value; } } ..... rest of the class .... } //the enum public enum Licence { FivePersonCar = 5, SixPersonCar = 6 } 

—————-这里是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 { 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() .WithTableName("InsuredWith"); } } 

—–这会生成以下HBM文件

                   

这是我的错误

“来自表DriverLicence的关联引用了一个未映射的类:Taxi.DomainObjects.Licence”

有谁知道我做错了什么?

枚举被NHibernate视为原始类型,因此不应使用带有class attributemany-to-many映射。 用.hbm来说,你需要这样的东西:

     

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

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

将我所做的翻译成你的代码:

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

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

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

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

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