Mapping enumerations

An improperly mapped enumeration can lead to unnecessary updates. In this recipe, we'll discuss why and show you how to map an enumeration property to a string field.

How to do it…

  1. Add a new folder named Enumerations to the MappingRecipes project.
  2. Add the following AccountTypes enum to the folder:
    public enum AccountTypes
    {
      Consumer,
      Business,
      Corporate,
      NonProfit
    }
  3. Add the following Account class:
    public class Account
    {  
      public virtual Guid Id { get; set; }
      public virtual AccountTypes AcctType { get; set; }
      public virtual string Number { get; set; }
      public virtual string Name { get; set; }
    }
  4. Add an embedded NHibernate mapping document named Account.hbm.xml with the following class mapping:
    <class name="Account"> <id name="Id"> <generator ...

Get NHibernate 4.x Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.