使用流利的nhibernate映射枚举

Pun*_*eet 32 .net enums fluent-nhibernate

我正在关注http://wiki.fluentnhibernate.org/Getting_started教程,用Fluent NHibernate创建我的第一个NHibernate项目

我有2张桌子

1)帐户与字段

Id
AccountHolderName
AccountTypeId
Run Code Online (Sandbox Code Playgroud)

2)带有字段的AccountType

Id
AccountTypeName
Run Code Online (Sandbox Code Playgroud)

现在,帐户类型可以是Savings或Current所以表AccountTypes存储2行1 - Savings 2 - Current

对于AccoutType表,我已经定义了枚举

public enum AccountType {
    Savings=1,
    Current=2
}
Run Code Online (Sandbox Code Playgroud)

对于Account表,我定义了实体类

public class Account {
    public virtual int Id {get; private set;}
    public virtual string AccountHolderName {get; set;}
    public virtual string AccountType {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

流畅的nhibernate映射是:

public AgencyMap() {
    Id(o => o.Id);
    Map(o => o.AccountHolderName);
    Map(o => o.AccountType);
}
Run Code Online (Sandbox Code Playgroud)

当我尝试运行解决方案,它提供了一个异常 - 的InnerException = {"(XmlDocument的)(2,4):XML验证错误:元素'类’在命名空间'瓮:NHibernate的映射-2.2’具有不完整的内容.期望的可能元素列表:'命名空间'中的元,子选择,缓存,同步,注释,tuplizer,id,composite-id'...

我想那是因为我还没有为AccountType指定任何映射.

问题是:

  1. 如何使用AccountType枚举而不是AccountType类?
  2. 也许我走错了路.有一个更好的方法吗?

谢谢!

Dan*_*ann 63

以下显然不再有效/sf/answers/35232921/

这样做怎么样:

public AgencyMap() {
    Id(o => o.Id);
    Map(o => o.AccountHolderName);
    Map(o => o.AccountType).CustomType<AccountType>();
}
Run Code Online (Sandbox Code Playgroud)

自定义类型处理一切:)

  • AcountType是一个枚举,因此加载后所有实体都将变脏.请参阅http://stackoverflow.com/questions/3531937/enum-to-integer-mapping-causing-updates-on-every-flush (8认同)
  • 这似乎是正确的,但会生成"幻影"更新.正确的映射是:CustomType <AccountType>.否则,就像schoetbi所说,加载后实体很脏.来源:https://groups.google.com/forum/#!searchin/fluent-nhibernate/enum/fluent-nhibernate/bBXlDRvphDw/AFnYs9ei7O0J (7认同)

mhe*_*xon 37

public class Account {
    public virtual int Id {get; private set;}
    public virtual string AccountHolderName {get; set;}
    public virtual AccountType AccountType {get; set;}
}

public AgencyMap() {
    Id(o => o.Id);
    Map(o => o.AccountHolderName);
    Map(o => o.AccountType);
}
Run Code Online (Sandbox Code Playgroud)

如果要覆盖需要为其提供约定,Fluent NHibernate默认将枚举值保存为字符串.就像是:

public class EnumConvention :
    IPropertyConvention, 
    IPropertyConventionAcceptance
{
    #region IPropertyConvention Members

    public void Apply(IPropertyInstance instance)
    {
        instance.CustomType(instance.Property.PropertyType);
    }

    #endregion

    #region IPropertyConventionAcceptance Members

    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(x => x.Property.PropertyType.IsEnum ||
        (x.Property.PropertyType.IsGenericType && 
         x.Property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
         x.Property.PropertyType.GetGenericArguments()[0].IsEnum)
        );
    }

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

几乎忘了您还需要将常规添加到您的流畅配置中.您可以在添加映射的同一位置执行此操作:

.Mappings(m => m.FluentMappings.AddFromAssemblyOf<BillingRecordMap>()
.Conventions.AddFromAssemblyOf<EnumConvention>()
Run Code Online (Sandbox Code Playgroud)

  • 请参阅此文章/评论,了解如何将其扩展为支持可以为空的枚举:http://stackoverflow.com/questions/439003/how-do-you-map-an-enum-as-an-int-value-with-fluent -nhibernate/2716236#2716236 (3认同)