Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 映射到枚举列表?_C#_Nhibernate_Orm_Fluent Nhibernate - Fatal编程技术网

C# 映射到枚举列表?

C# 映射到枚举列表?,c#,nhibernate,orm,fluent-nhibernate,C#,Nhibernate,Orm,Fluent Nhibernate,我需要使用NHibernate将具有枚举列表的类映射到db表 这些是物品 public class Driver : IIdentity { private IList<Licence> licences; /// <summary> /// The drivers licences /// </summary> public virtual IList<Licence> Licences {

我需要使用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
}
-----这将生成以下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.License”


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

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

要将我所做的转换为您的代码:

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

public virtual LicenseCode LicenseCode 
{
     get
     {
         return (LicenseCode)LicenseId;
     }
}
<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>
然后,创建一个表以匹配您将为此对象存储的数据(id和名称)


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

枚举被NHibernate视为基本类型,因此您不应该使用带有class
属性的
多对多
进行映射。在.hbm术语中,您需要以下内容:

public virtual LicenseCode LicenseCode 
{
     get
     {
         return (LicenseCode)LicenseId;
     }
}
<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映射中,您可以省略long
type
属性。我流利的语法不是很好,所以恐怕我帮不了你。可能会有进一步的帮助。

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

有许多(x=>x.licenses)。WithTableName(“DriverLicence”).AsElement(“Level”).AsBag();谢谢你的回答,我在你的帖子上回复了,并留下了一个很好的链接。我不知道你的意思,我想要一个驾驶执照列表(Enum)(这是一个演示应用程序)。enum(许可证)上的标志arrtibute将如何作为替代方案?如果这比我使用的senario更好,请随时通知我。否则主要问题就解决了