C# 如何使用NHibernate使用枚举?
在AccountType.cs中,我有:C# 如何使用NHibernate使用枚举?,c#,nhibernate,enums,C#,Nhibernate,Enums,在AccountType.cs中,我有: namespace MooDB.Domain { public enum AccountType {Real, Demo, Fictional}; } public class Account : Entity { public virtual int Id { get; set; } public virtual Broker Broker { get; set; } public virtual st
namespace MooDB.Domain
{
public enum AccountType {Real, Demo, Fictional};
}
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.cs中,我有:
namespace MooDB.Domain
{
public enum AccountType {Real, Demo, Fictional};
}
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>
当我尝试测试对帐户的检索时,我从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.
我所要做的就是在我的应用程序中有一个帐户类型列表,以便进行验证。我不想在数据库中有查找表。知道我做错了什么吗?我认为NHibernate默认将枚举作为整数持久化(因为在.net中,枚举基本上是语法上的糖分,而不是整数),除非您告诉它。如果您真的需要它成为字符串,那么您需要实现一个IUserType。类似的例子是将字符串映射为布尔和
使用Enum,您只需更改NullSafeGet和NullSafeSet中的逻辑即可映射到您的Enum,可能需要使用Enum.Parse或TryParse方法。在AccountType映射中,使用以下值添加
type
属性
NHibernate.Type.EnumStringType`1[[MooDB.Domain.AccountType, MooDB]], NHibernate
通过为AccountType指定type
属性,我们告诉NHibernate使用自定义
类以在.NET类型和数据库之间进行转换。NHibernate包括
EnumStringType
覆盖枚举值到数据库的转换
值,以便存储字符串名称,而不是数字值