映射到枚举列表? [英] Map to a list of Enums?

查看:105
本文介绍了映射到枚举列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要映射具有枚举的列表,以一个数据库表一类,使用NHibernate

I need to map a class which has a list of Enums to a db table, using 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
}

----------------这里是数据库表

---------------- here is the DB table

TABLE [DBO ]。[DriverLicence(
[DriverId] [INT] NOT NULL,
[等级] [INT] NOT NULL)

TABLE [DBO]。[司机](
[DriverId] [INT] NOT NULL,
[名] [VARCHAR(150)NULL)

-------------这里是我的司机说流利的地图

-------------Here is my 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文件

----- This generates the following HBM file

<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

"An association from the table DriverLicence refers to an unmapped class: Taxi.DomainObjects.Licence"

有谁知道什么即时做错了什么?

anyone know what im doing wrong?

推荐答案

枚举被认为是一种最原始的由NHibernate的,所以你不应该映射使用多到许多带班属性。在.hbm而言,你需要这样的:

Enums are considered a primitive type by NHibernate, so you should not map using a many-to-many with a class attribute. In .hbm terms, you'd need something like this:

<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映射可以省略长键入属性。我流利的语法不是很好,所以我不能帮你那里,我很害怕。 这个问题可进一步帮助。

Although in hbm mapping like this you can omit the long type attribute. My Fluent syntax is not very good, so I can't help you there I'm afraid. This question may help further.

这篇关于映射到枚举列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆