Mapping a collection of enums with NHibernate

Posted by beaufabry on Stack Overflow See other posts from Stack Overflow or by beaufabry
Published on 2008-09-17T01:11:21Z Indexed on 2010/05/11 2:54 UTC
Read the original article Hit count: 510

Mapping a collection of enums with NHibernate

Specifically, using Attributes for the mappings.

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.

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.

I found a post that said to define a class as

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

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

So has anyone got experience doing this?

So anyway, just a simple reference code snippet to give an example with

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

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.

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about nhibernate-mapping