<p>Mapping a collection of enums with NHibernate</p>

<p>Specifically, using Attributes for the mappings.</p>

<p>Currently I have this working mapping the collection as type Int32 and NH seems to take care of it, but it's not exactly ideal.</p>

<p>The error I receive is "Unable to determine type" when trying to map the collection as of the type of the enum I am trying to map.</p>

<p>I found a post that said to define a class as</p>

<pre><code>public class CEnumType : EnumStringType {
  public CEnumType() : base(MyEnum) { }
}
</code></pre>

<p>and then map the enum as CEnumType, but this gives "CEnumType is not mapped" or something similar.</p>

<p>So has anyone got experience doing this?</p>

<p>So anyway, just a simple reference code snippet to give an example with</p>

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

        private IList&lt;EAction&gt; m_class_actions = new List&lt;EAction&gt;();

        [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&lt;EAction&gt; Actions
        {
            get { return m_class_actions; }
            set { m_class_actions = value;}
        }
}
</code></pre>

<p>So, anyone got the correct attributes for me to map this collection of enums as actual enums? It would be really nice if they were stored in the db as strings instead of ints too but it's not completely necessary.</p>

[{'answer_id': 80485, 'author': 'alvin', 'author_id': 15121, 'author_profile': 'https://Stackoverflow.com/users/15121', 'pm_score': 1, 'selected': False, 'text': '<p>This is the way i do it. There\'s probably an easier way but this works for me.</p>\n\n<p>Edit: sorry, i overlooked that you want it as a list. I don\'t know how to do that...</p>\n\n<p>Edit2: maybe you can map it as a protected IList[string], and convert to public IList[EAction] just as i do with a simple property.</p>\n\n<pre><code>    public virtual ContractGroups Group\n    {\n        get\n        {\n            if (GroupString.IsNullOrEmpty())\n                return ContractGroups.Default;\n\n            return GroupString.ToEnum&lt;ContractGroups&gt;(); // extension method\n        }\n        set { GroupString = value.ToString(); }\n    }\n\n    // this is castle activerecord, you can map this property in NH mapping file as an ordinary string\n    [Property("`Group`", NotNull = true)] \n    protected virtual string GroupString\n    {\n        get;\n        set;\n    }\n\n\n\n    /// &lt;summary&gt;\n    /// Converts to an enum of type &lt;typeparamref name="TEnum"/&gt;.\n    /// &lt;/summary&gt;\n    /// &lt;typeparam name="TEnum"&gt;The type of the enum.&lt;/typeparam&gt;\n    /// &lt;param name="self"&gt;The self.&lt;/param&gt;\n    /// &lt;returns&gt;&lt;/returns&gt;\n    /// &lt;remarks&gt;From &lt;see href="http://www.mono-project.com/Rocks"&gt;Mono Rocks&lt;/see&gt;.&lt;/remarks&gt;\n    public static TEnum ToEnum&lt;TEnum&gt;(this string self)\n        where TEnum : struct, IComparable, IFormattable, IConvertible\n    {\n        Argument.SelfNotNull(self);\n\n        return (TEnum)Enum.Parse(typeof(TEnum), self);\n    }\n</code></pre>\n'}, {'answer_id': 214268, 'author': 'kͩeͣmͮpͥ ͩ', 'author_id': 26479, 'author_profile': 'https://Stackoverflow.com/users/26479', 'pm_score': 1, 'selected': False, 'text': '<p>instead of</p>\n\n<pre><code>[NHibernate.Mapping.Attributes.Element(2, Column = "EAction", Type = "Int32")]\n</code></pre>\n\n<p>try</p>\n\n<pre><code>[NHibernate.Mapping.Attributes.Element(2, Column = "EAction", Type = "String")]\n</code></pre>\n\n<p>ie: change the <code>Int32</code> to <code>String</code></p>\n'}, {'answer_id': 271296, 'author': 'Geoff Bennett', 'author_id': 35377, 'author_profile': 'https://Stackoverflow.com/users/35377', 'pm_score': 1, 'selected': False, 'text': '<p>While I haven\'t tried using it myself, I stumbled across this code a little while ago and it looks pretty interesting:</p>\n\n<p><a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/08/12/enumeration-classes.aspx" rel="nofollow noreferrer" title="Jimmy Bogard - Enumeration Classes">http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/08/12/enumeration-classes.aspx</a></p>\n\n<p>Like I said, I haven\'t used it myself, but I\'m going to give it a go in a project RSN.</p>\n'}, {'answer_id': 2807763, 'author': 'Lisa', 'author_id': 314283, 'author_profile': 'https://Stackoverflow.com/users/314283', 'pm_score': 2, 'selected': False, 'text': '<p>You will need to map your CEnum type directly. In XML mappings this would mean creating a new class mapping element in your NHibernate XML mappings file.</p>\n\n<p><code><pre></p>\n\n<pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="YourAssembly"\n   auto-import="true" default-lazy="false"&gt;\n\n   ...\n\n   &lt;class name="YourAssemblyNamespace.CEnum" table="CEnumTable" mutable="false" &gt;\n      &lt;id name="Id" unsaved-value="0" column="id"&gt;\n         &lt;generator class="native"/&gt;\n      &lt;/id&gt;\n\n      ...\n\n   &lt;/class&gt;\n\n&lt;/hibernate-mapping&gt;\n</code></pre>\n\n<p></pre></code></p>\n\n<p>To do it with attribute mappings, something like this on top of your CEnum class:</p>\n\n<p><code>[NHibernate.Mapping.Attributes.Class(Table = "CEnumTable")] //etc as you require</code></p>\n'}]
['https://Stackoverflow.com/questions/78852', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/924607/']