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…
Add a new folder named
Enumerations
to theMappingRecipes
project.Add the following
AccountTypes
enum to the folder:public enum AccountTypes { Consumer, Business, Corporate, NonProfit }
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; } }
Add an embedded NHibernate mapping document named
Account.hbm.xml
with the following class mapping:<class name="Account"> <id name="Id"> <generator class="guid.comb" /> </id> <natural-id> <property name="Number" not-null="true" /> </natural-id> <property name="Name" not-null="true" /> <property name...