Maps a Enum to a DbType.String.

Namespace: NHibernate.Type
Assembly:   NHibernate (in NHibernate)
Version: 1.2.0.4000

Syntax

Visual Basic (Declaration)
<SerializableAttribute> _
Public MustInherit Class EnumStringType _
	Inherits ImmutableType _
	Implements IDiscriminatorType, IIdentifierType, IType, ICacheAssembler, ILiteralType
C#
[SerializableAttribute]
public abstract class EnumStringType : ImmutableType, IDiscriminatorType, IIdentifierType, IType, ICacheAssembler, ILiteralType
Visual C++
[SerializableAttribute]
public ref class EnumStringType abstract : public ImmutableType, IDiscriminatorType, IIdentifierType, IType, ICacheAssembler, ILiteralType

Remarks

If your database should store the Enum using the named values in the enum instead of the underlying values then subclass this IType.

All that needs to be done is to provide a default constructor that NHibernate can use to create the specific type. For example, if you had an enum defined as.

CopyC#
public enum MyEnum 
{
    On,
    Off,
    Dimmed
}

all that needs to be written for your enum string type is:

CopyC#
public class MyEnumStringType : NHibernate.Type.EnumStringType
{
    public MyEnumStringType()
        : base( typeof( MyEnum ) )
    {
    }
}

The mapping would look like:

CopyC#
...
    <property name="Status" type="MyEnumStringType, AssemblyContaining" />
...

The TestFixture that shows the working code can be seen in NHibernate.Test.TypesTest.EnumStringTypeFixture.cs , NHibernate.Test.TypesTest.EnumStringClass.cs , and NHibernate.Test.TypesTest.EnumStringClass.hbm.xml

Inheritance Hierarchy

System.Object
  NHibernate.Type.AbstractType
    NHibernate.Type.NullableType
      NHibernate.Type.ImmutableType
        NHibernate.Type.EnumStringType