如何使用NHibernate枚举? [英] How to work with Enums with NHibernate?

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

问题描述

在AccountType.cs我有:

In AccountType.cs I have:

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

在Account.cs我有:

In Account.cs I have:

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我有

In my Account.hbm.xml I have

<?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>



我的数据库表如下所示:

My database table looks like:

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的得到这个错误:

When I try and test a retrieval of an account, I get this error from 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.



所有我想要做的就是在我的用于验证应用程序帐户类型的列表。我不希望有一个查找表在我的数据库。任何想法我做错了吗?

All I want to do is have a list of Account Types in my application for validation purposes. I don't want to have a lookup table in my DB. Any ideas what I'm doing wrong?

推荐答案

在ACCOUNTTYPE映射添加键入与下面的值

On the AccountType mapping add the type attribute with the following value

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

通过指定键入属性对于ACCOUNTTYPE,我们告诉NHibernate的使用自定义的
类.NET类型和数据库之间的转换。 NHibernate的包括
EnumStringType< T> 覆盖枚举值的数据库
值的转换,因此字符串名称存储,而不是数值。

By specifying a type attribute for AccountType, we tell NHibernate to use a custom class for conversion between .NET types and the database. NHibernate includes EnumStringType<T> to override the conversion of enumeration values to database values so that the string name is stored, not the numeric value.

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

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