用流畅的nhibernate映射枚举 [英] Mapping enum with fluent nhibernate
问题描述
我正在关注 http://wiki.fluentnhibernate.org/Getting_started 教程,以创建我的第一个NHibernate项目与Fluent NHibernate
I am following the http://wiki.fluentnhibernate.org/Getting_started tutorial to create my first NHibernate project with Fluent NHibernate
我有2个表
1)帐户与字段
Id
AccountHolderName
AccountTypeId
2)帐户类型字段
Id
AccountTypeName
现在帐户类型可以是储蓄或当前
所以表AccountTypes存储2行
1 - 储蓄
2 - 当前
Right now the account types can be Savings or Current So the table AccountTypes stores 2 rows 1 - Savings 2 - Current
对于AccoutType表,我已定义枚举
For AccoutType table I have defined enum
public enum AccountType {
Savings=1,
Current=2
}
对于帐户表我定义实体类
For Account table I define the entity class
public class Account {
public virtual int Id {get; private set;}
public virtual string AccountHolderName {get; set;}
public virtual string AccountType {get; set;}
}
流畅的nhibernate映射是:
The fluent nhibernate mappings are:
public AgencyMap() {
Id(o => o.Id);
Map(o => o.AccountHolderName);
Map(o => o.AccountType);
}
当我尝试运行解决方案时,会产生异常 - InnerException = { (XmlDocument)(2,4):XML验证错误:命名空间'urn:nhibernate-mapping-2.2'中的元素'类'具有不完整的内容。可能的元素列表:元,子选择,缓存,同步,注释,tuplizer,id,composite-id'in namespace'ur ...
When I try to run the solution, it gives an exception - InnerException = {"(XmlDocument)(2,4): XML validation error: The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has incomplete content. List of possible elements expected: 'meta, subselect, cache, synchronize, comment, tuplizer, id, composite-id' in namespace 'ur...
我猜这是因为我没有提供任何帐户类型的映射。
I guess that is because I have not speciofied any mapping for AccountType.
问题是:
- 如何使用AccountType枚举
而不是AccountType课程? - 也许我走错了吗?有更好的方法吗?
谢谢!
推荐答案
以下显然不再起作用 https://stackoverflow.com/a/503327/189412
:
public AgencyMap() {
Id(o => o.Id);
Map(o => o.AccountHolderName);
Map(o => o.AccountType).CustomType<AccountType>();
}
自定义类型处理所有内容:)
The custom type handles everything :)
这篇关于用流畅的nhibernate映射枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!