【问题标题】:How to work with Enums with NHibernate?如何使用 NHibernate 使用枚举?
【发布时间】:2012-05-10 01:00:20
【问题描述】:

在 AccountType.cs 我有:

namespace MooDB.Domain
{
    public enum AccountType {Real, Demo, Fictional};
}

在 Account.cs 我有:

public class Account : Entity
{        
    public virtual int Id { get; set; }
    public virtual Broker Broker { get; set; }
    public virtual string Name { get; set; }
    public virtual string NickName { get; set; }
    public virtual string AccountNumber { get; set; }
    public virtual Currency Currency { get; set; }
    public virtual AccountType AccountType { get; set; }
    public virtual bool IsActive { get; set; }
    public virtual bool IsDefault { get; set; }
}

在我的 Account.hbm.xml 我有

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="MooDB"
                   namespace="MooDB.Domain">

  <class name="MooDB.Domain.Account,MooDB" table="accounts">
    <id name="Id" column="accountId" type="Int32" unsaved-value="0">
      <generator class="native" />
    </id>
    <version name="Version" column="version" type="integer" unsaved-value="0" />
    <many-to-one name ="Broker" column="brokerId" not-null="true" class="MooDB.Domain.Broker,MooDB" />
    <property name="Name" column="`name`" type="String" length="50" not-null="true" />
    <property name="NickName" column="nickName" type="String" length="50" not-null="true" />
    <property name="AccountNumber" column="accountNumber" type="String" length="50" not-null="true" />
    <many-to-one name ="Currency" column="currency" not-null="true" class="MooDB.Domain.Currency,MooDB" />
    <property name="AccountType" column="accountType" />
    <property name="IsActive" column="isActive" type="bool" not-null="true" />
    <property name="IsDefault" column="isDefault" type="bool" not-null="true" />
  </class>
</hibernate-mapping>

我的数据库表如下:

CREATE TABLE [dbo].[accounts](
    [accountId] [int] IDENTITY(1,1) NOT NULL,
    [brokerId] [int] NOT NULL,
    [name] [nvarchar](50) NOT NULL,
    [nickName] [nvarchar](50) NOT NULL,
    [accountNumber] [nvarchar](50) NOT NULL,
    [currency] [char](3) NOT NULL,
    [accountType] [varchar](10) NOT NULL,
    [isActive] [bit] NOT NULL,
    [isDefault] [bit] NOT NULL,
    [version] [int] NOT NULL,
 CONSTRAINT [PK_accounts] PRIMARY KEY CLUSTERED 
(
    [accountId] ASC
)

当我尝试测试帐户检索时,我从 NHibernate 收到此错误:

Test 'Test.RepoTest.CanGetAccountById' failed: NHibernate.Exceptions.GenericADOException : could not load an entity: [MooDB.Domain.Account#1][SQL: SELECT account0_.accountId as accountId3_0_, account0_.version as version3_0_, account0_.brokerId as brokerId3_0_, account0_.[name] as name4_3_0_, account0_.nickName as nickName3_0_, account0_.accountNumber as accountN6_3_0_, account0_.currency as currency3_0_, account0_.accountType as accountT8_3_0_, account0_.isActive as isActive3_0_, account0_.isDefault as isDefault3_0_ FROM accounts account0_ WHERE account0_.accountId=?]
  ----> System.FormatException : Input string was not in a correct format.

我想要做的就是在我的应用程序中列出一个帐户类型以用于验证目的。我不想在我的数据库中有一个查找表。任何想法我做错了什么?

【问题讨论】:

    标签: c# nhibernate enums


    【解决方案1】:

    在 AccountType 映射中添加具有以下值的 type 属性

    NHibernate.Type.EnumStringType`1[[MooDB.Domain.AccountType, MooDB]], NHibernate
    

    通过为 AccountType 指定 type 属性,我们告诉 NHibernate 使用自定义 .NET 类型和数据库之间的转换类。 NHibernate 包括 EnumStringType&lt;T&gt; 覆盖枚举值到数据库的转换 值,以便存储字符串名称,而不是数值。

    【讨论】:

      【解决方案2】:

      我认为 NHibernate 会默认将枚举持久化为整数(因为在 .net 中,枚举基本上是整数的语法糖),除非您另有说明。如果你真的需要它作为字符串,那么你需要实现一个 IUserType。一个类似的例子是将字符串映射到布尔值和is covered here

      使用枚举,您只需更改 NullSafeGet 和 NullSafeSet 中的逻辑以映射到您的枚举,可能使用 Enum.Parse 或 TryParse 方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-01
        • 2011-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多