用NHibernate映射枚举集合

时间:2020-03-05 18:57:38  来源:igfitidea点击:

用NHibernate映射枚举集合

具体来说,使用属性进行映射。

目前,我已经完成了将集合映射为Int32类型的工作,并且NH似乎可以解决这个问题,但这并不是完全理想。

尝试将集合映射为我要映射的枚举类型时,收到的错误是"无法确定类型"。

我发现有一条帖子说要定义一个类为

public class CEnumType : EnumStringType {
  public CEnumType() : base(MyEnum) { }
}

然后将枚举映射为CEnumType,但这给出了"未映射CEnumType"或者类似的内容。

那么,有没有人有这样做的经验?

因此,无论如何,仅是一个简单的参考代码段即可提供示例

[NHibernate.Mapping.Attributes.Class(Table = "OurClass")]
    public class CClass : CBaseObject
    {
        public enum EAction
        {
            do_action,
            do_other_action
        };

        private IList<EAction> m_class_actions = new List<EAction>();

        [NHibernate.Mapping.Attributes.Bag(0, Table = "ClassActions", Cascade="all", Fetch = CollectionFetchMode.Select, Lazy = false)]
        [NHibernate.Mapping.Attributes.Key(1, Column = "Class_ID")]
        [NHibernate.Mapping.Attributes.Element(2, Column = "EAction", Type = "Int32")]
        public virtual IList<EAction> Actions
        {
            get { return m_class_actions; }
            set { m_class_actions = value;}
        }
}

因此,有人为我提供了正确的属性,可以将此枚举集合映射为实际枚举吗?如果它们也以字符串而不是ints的形式存储在数据库中,那将是非常好的,但这不是完全必要的。

解决方案

回答

这就是我做到的方式。可能有一种更简单的方法,但这对我有用。

编辑:对不起,我忽略了我们想要它作为列表。我不知道该怎么做...

Edit2:也许我们可以将其映射为受保护的IList [string],并转换为公共IList [EAction],就像我使用简单属性一样。

public virtual ContractGroups Group
    {
        get
        {
            if (GroupString.IsNullOrEmpty())
                return ContractGroups.Default;

            return GroupString.ToEnum<ContractGroups>(); // extension method
        }
        set { GroupString = value.ToString(); }
    }

    // this is castle activerecord, you can map this property in NH mapping file as an ordinary string
    [Property("`Group`", NotNull = true)] 
    protected virtual string GroupString
    {
        get;
        set;
    }

    /// <summary>
    /// Converts to an enum of type <typeparamref name="TEnum"/>.
    /// </summary>
    /// <typeparam name="TEnum">The type of the enum.</typeparam>
    /// <param name="self">The self.</param>
    /// <returns></returns>
    /// <remarks>From <see href="http://www.mono-project.com/Rocks">Mono Rocks</see>.</remarks>
    public static TEnum ToEnum<TEnum>(this string self)
        where TEnum : struct, IComparable, IFormattable, IConvertible
    {
        Argument.SelfNotNull(self);

        return (TEnum)Enum.Parse(typeof(TEnum), self);
    }

回答

代替

[NHibernate.Mapping.Attributes.Element(2, Column = "EAction", Type = "Int32")]

尝试

[NHibernate.Mapping.Attributes.Element(2, Column = "EAction", Type = "String")]

即:将Int32更改为String。

回答

虽然我自己还没有尝试使用它,但不久前我偶然发现了这段代码,它看起来很有趣:

http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/08/12/enumeration-classes.aspx

就像我说的那样,我自己并没有使用它,但是我将在RSN项目中尝试一下。

回答

我们将需要直接映射CEnum类型。在XML映射中,这意味着在NHibernate XML映射文件中创建一个新的类映射元素。

`


爪哇
<hibernate-mapping xmlns =" urn:nhibernate-mapping-2.2" assembly =" YourAssembly"
auto-import =" true" default-lazy =" false">
...
<class name =" YourAssemblyNamespace.CEnum" table =" CEnumTable" mutable =" false">
<id name =" Id"未保存值=" 0" column =" id">
<generator class =" native" />
</ id>
...
</ class>
</ hibernate-mapping>

`

要使用属性映射来做到这一点,请在CEnum类的顶部进行如下操作:

`[NHibernate.Mapping.Attributes.Class(Table =" CEnumTable")] //根据需要等