title: Using NHibernate to map a nChar column to an enumerated type
body: <p>I am trying to map a table frp, a SQL Server 2005 DB to a class which contains an enum: </p>
  
  <pre><code>public class MyClass{
  private YesNoOptional addressSetting;
  public YesNoOptional AddressSetting{
      {get; set;}
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>public enum YesNoOptional {
      Yes,
      No,
      Optional
  }
  </code></pre>
  
  <p>This will dictate that one of three values is inserted into the corresponding column - 'Y', 'N', 'O'. This column is of type nchar(1).</p>
  
  <p>My mapping file is like so (addressSetting is the property which is causing the problem):</p>
  
  <pre><code>  &lt;class name="IncidentDefinition" table="IR_INCIDENT_DEF" lazy="false" &gt;
      &lt;id name="Guid" column="INT_GUID" &gt;
        &lt;generator class="guid"&gt;&lt;/generator&gt;
      &lt;/id&gt;
      &lt;property name="Reference" column="INT_REF" &gt;&lt;/property&gt;
      &lt;property name="Description" column="INT_DESCRIPTION"  &gt;&lt;/property&gt;
  
      &lt;property name="AddressSetting" column="INT_ADDRESS_REQ" &gt;&lt;/property&gt;
  
      &lt;property name="Active" column="INT_ACTIVE" type="YesNo"&gt;&lt;/property&gt;
      &lt;property name="PinDocId" column="INT_PIN_DOC_ID"&gt;&lt;/property&gt;
    &lt;/class&gt;  
  </code></pre>
  
  <p>Using the config above I get the following error: 
  NHibernate.ADOException: could not initialize a collection: System.FormatException: Input string was not in a correct format..</p>
  
  <p>If I try to map the enum using a custom type like so:</p>
  
  <pre><code>&lt;property name="AddressSetting" column="INT_ADDRESS_REQ" type="ML.Types.YesNoOptional, ML.Types" &gt;&lt;/property&gt;
  </code></pre>
  
  <p>Error: System.FormatException: Input string was not in a correct format.</p>
  
  <p>Next, if I try using a specific type like so:</p>
  
  <pre><code> &lt;property name="AddressSetting" column="INT_ADDRESS_REQ" type="String" &gt;&lt;/property&gt;
  </code></pre>
  
  <p>This error is generated: NHibernate.PropertyAccessException: The type System.String can not be assigned to a property of type System.ArgumentException: Object of type 'System.String' cannot be converted to type 'ML.Types.YesNoOptional'..</p>
  
  <p>As a last resort I tried to specify the type as a char like so:</p>
  
  <pre><code>&lt;property name="AddressSetting" column="INT_ADDRESS_REQ" type="Char" &gt;&lt;/property&gt;
  </code></pre>
  
  <p>This works a bit better, as it in it doesnt throw an error, however instead of returning the character from the table and mapping it to the enumerated type the ASCII value of the character is returned instead - so Y is represented by 89!</p>
  
  <p>I am hoping someone can explain what I am doing wrong and\or how why this is happening please?</p>
  
  <p>Thanks</p>
  
  <p>Morris</p>