Maps a Enum to a DbType.String.

Namespace: NHibernate.Type
Assembly: NHibernate (in NHibernate.dll) Version: 3.2.0.4000 (3.2.0.4000)

Syntax

C#
[SerializableAttribute]
public abstract class EnumStringType : AbstractEnumType
Visual Basic
<SerializableAttribute> _
Public MustInherit Class EnumStringType _
	Inherits AbstractEnumType
Visual C++
[SerializableAttribute]
public ref class EnumStringType abstract : public AbstractEnumType

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

See Also