Devart Blog

String Enum Representation in Entity Developer

Posted by on November 17th, 2014

This article explains and demonstrates mapping a string representation of enum to string fields in a database in Entity Developer. The sample, demonstrated in this post, is included into our NHibernate Mapping Samples application, described in the corresponding NHibernate Mapping Samples blog article. It is named ‘Enum Type Property'; however, other samples from this application probably prove to become useful for NHibernate users both beginners and professionals, so we recommend downloading it for everyone who develop NHibernate applications.

The problem with mapping enum types in NHibernate is that NHibernate can map enum type values only to the numeric columns in database without additional manipulations. However, when enum values are stored in database as strings, NHibernate requires the user to perform some additional actions.

Our sample uses the following database table:

  EmployeeType INTEGER NOT NULL,
  FirstName VARCHAR(50) NOT NULL,
  LastName VARCHAR(50) NOT NULL,

The EmployeeType and Sex fields of the corresponding Employee class, must be mapped to the corresponding enum types:

    public enum EmployeeType : int
        Manager = 1,    
        Clerk = 2,    
        Courier = 3

    public enum SexType : int

EmployeeType values are stored in the database in the numeric format, so it’s easy to map this field. It is defined in the XML file as following:

    <property name="EmployeeType" type="EmployeeType">
      <column name="EmployeeType" not-null="true" sql-type="INTEGER" />

The Sex field stores its enum values in the database as strings, and standard mapping cannot be used in this case. You need to define an additional class, which inherits from the Nhibernate.Type.EnumStringType class from the NHibernate.dll assembly, for each of such enums:

    public class SexTypeEnumStringType : NHibernate.Type.EnumStringType
        public SexTypeEnumStringType() : base(typeof(SexType))

And then you must use this class for mapping this property in the XML file:

    <property name="Sex" type="SexTypeEnumStringType">
      <column name="Sex" not-null="true" length="20" sql-type="VARCHAR" />

Thus, storing enum values as strings in the database requires an additional class for each of such enums, and redefining the type in mapping of each property of such enum type. This means additional time costs and inconveniences.

With Entity Developer for NHibernate, you can easily get rid of all these inconveniences. In our example, it’s enough to select the Sex field of the Employee class and, in the Properties window, switch its Enum Storage Mode property to String. After this Entity Developer will generate all the necessary classes and specify them in the generated mapping automatically.

Default Enum Storage Property

If you always store enum values in the database as strings, you can set Enum Storage Mode for all the entity properties in the model instead of manually setting it for each property manually. For this, open the Model Settings dialog box (you can do it by right-clicking the root model node in Model Explorer or empty space on a diagram and selecting Model Settings in the shortcut menu), and on the General page set the required value for Default Enum Storage. This changes the default value of the Default Enum Storage property for all entity properties, for which the value of this setting was not changed manually.

Default Enum Storage in Model Settings

If you want to use an enum value mapping strategy different from the model default one for some property, you can always specify it by setting Enum Storage Mode for this property to the corresponding value.

One Response to “String Enum Representation in Entity Developer”

  1. Clara Says:

    Learning a ton from these neat arcleits.

Leave a Reply