枚举解析似乎不适用于 Fluent NHibernate

发布于 2024-09-09 10:04:10 字数 415 浏览 7 评论 0原文

我有一个带有名为 Salutation 的枚举的数据访问类:

 public enum Salutation
  {
    Unknown = 0,
    Dame = 1,
    etc
    Mr = 5,
    etc
  }

我正在使用 NHibernate 保留该类,直到今天早上我还在使用 .hbm.xml 文件进行映射。但是,我现在已改用 Fluent NHibernate,但加载该类的实例失败(例如):

[HibernateException: Can't Parse 5 as Salutation]

如您所见,5 应该可以解析为 Salutation(假设5是一个int,从错误消息中无法看出)。

有人知道这是怎么回事吗?

谢谢

大卫

I have a data access class with an Enum called Salutation:

 public enum Salutation
  {
    Unknown = 0,
    Dame = 1,
    etc
    Mr = 5,
    etc
  }

I am peristing the class with NHibernate, and up until this morning I was using .hbm.xml files for mapping. However, I've now switched to using Fluent NHibernate, but loading instances of the class fails with (for example):

[HibernateException: Can't Parse 5 as Salutation]

As you can see, 5 should be parseable as a Salutation (assuming 5 is an int, it's not possible to tell from the error message).

Anyone know what's going on here?

Thanks

David

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江挽川 2024-09-16 10:04:10

这比我想象的要容易得多。

Map(x => x.WhateverThePropertyNameIs).CustomType(typeof(MyEnumeration));

This is much easier than I thought.

Map(x => x.WhateverThePropertyNameIs).CustomType(typeof(MyEnumeration));
梦开始←不甜 2024-09-16 10:04:10

另一个选项是使用 EnumConvention:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestApp
{
    using FluentNHibernate.Conventions;
    using FluentNHibernate.Conventions.AcceptanceCriteria;
    using FluentNHibernate.Conventions.Inspections;
    using FluentNHibernate.Conventions.Instances;

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

要使用此 enumconvention:

...
var fluentCfg = Fluently.Configure().Database(cfg).Mappings(
                    m => m.FluentMappings.AddFromAssemblyOf<SomeObjectMap>().Conventions.Add<EnumConvention>());
...

在映射文件中,

Map(x => x.SomeEnumField);

Another option is using, EnumConvention:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestApp
{
    using FluentNHibernate.Conventions;
    using FluentNHibernate.Conventions.AcceptanceCriteria;
    using FluentNHibernate.Conventions.Inspections;
    using FluentNHibernate.Conventions.Instances;

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

To use this enumconvention:

...
var fluentCfg = Fluently.Configure().Database(cfg).Mappings(
                    m => m.FluentMappings.AddFromAssemblyOf<SomeObjectMap>().Conventions.Add<EnumConvention>());
...

And in mapping file,

Map(x => x.SomeEnumField);
波浪屿的海角声 2024-09-16 10:04:10

由于最受接受的答案仍然因相同的错误而失败,这里是我的解决方案:

public class ConventionName : IUserTypeConvention
{
    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(property => property.Type == typeof(FluentNHibernate.Mapping.GenericEnumMapper<EnumNameHere>));
    }

    public void Apply(IPropertyInstance instance)
    {
        instance.CustomType<EnumNameHere>();
    }
}

Since the most accepted answer does still fail with same error, here my solution:

public class ConventionName : IUserTypeConvention
{
    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(property => property.Type == typeof(FluentNHibernate.Mapping.GenericEnumMapper<EnumNameHere>));
    }

    public void Apply(IPropertyInstance instance)
    {
        instance.CustomType<EnumNameHere>();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文