代码之家  ›  专栏  ›  技术社区  ›  dbones

映射到枚举列表?

  •  3
  • dbones  · 技术社区  · 16 年前

    我需要使用nhibernate将一个包含枚举列表的类映射到一个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
    }
    

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

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

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

    ------------这是我给司机的流畅的地图

    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>
    

    这是我的错误

    “DriverLicense表中的关联引用了未映射的类:taxi.domainObjects.licence”

    有人知道我做错了什么吗?

    3 回复  |  直到 16 年前
        1
  •  5
  •   Community CDub    8 年前

    Enums被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
  •  0
  •   Community CDub    8 年前

    这正是我要的 here . 通过我自己的玩弄,我不知道如何使它工作,所以我最终不得不做一种工作。

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

    将您的许可证类名更改为其他名称(在本例中,我将使用licensecode),然后创建一个名为license的对象。至少在我的例子中,这个对象有一个ID和一个名称。ID实际上是在我的枚举中分配的整数值。如果需要,可以用同样的方法使用枚举的名称,只需修改下面的代码来转换名称而不是ID。在类中,添加如下属性:

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

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

    然后,创建映射。这将是直接的,因为您只映射ID和名称。

        3
  •  0
  •   Martin R-L    16 年前

    您是否考虑将flags属性与枚举类一起使用,而不是使用它们的列表?