枚举映射与功能NHibernate功能、NHibernate

2023-09-02 01:54:35 作者:安子陌先森

我下面的http://wiki.fluentnhibernate.org/Getting%5Fstarted教程与功能NHibernate创建我的第一个NHibernate的项目

I am following the http://wiki.fluentnhibernate.org/Getting%5Fstarted tutorial to create my first NHibernate project with Fluent NHibernate

我有2表

1)帐户域

Id
AccountHolderName
AccountTypeId

2)AccountType与领域

2) AccountType with fields

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验证错误:元素'类'的命名空间金塔:NHibernate的映射 - 2.2'有不完整的内容列出可能的要素预计:元,子查询,缓存,同步,评论,tuplizer,ID,复合-ID的命名空间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...

我想这是因为我没有任何speciofied映射AccountType。

I guess that is because I have not speciofied any mapping for AccountType.

的问题是:

如何使用AccountType枚举 而不是AccountType类? 也许我会在错误的轨道上。有没有更好的方式来做到这一点? How can I use AccountType enum instead of a AccountType class? Maybe I am going on wrong track. Is there a better way to do this?

谢谢!

推荐答案

怎么样只是在做这样的:

How about just doing this:

public AgencyMap() {
    Id(o => o.Id);
    Map(o => o.AccountHolderName);
    Map(o => o.AccountType).CustomType<int>();
}

自定义类型处理一切:)

The custom type handles everything :)