views:

39

answers:

1

Hi all

I have a data access class with an Enum called Salutation:

 public enum Salutation
  {
    Unknown = 0,
    Dame = 1,
    etc
    Mr = 5,
    etc
  }

I am peristing the class with NHibernate, and up until this morning I was using .hbm.xml files for mapping. However, I've now switched to using Fluent NHibernate, but loading instances of the class fails with (for example):

[HibernateException: Can't Parse 5 as Salutation]

As you can see, 5 should be parseable as a Salutation (assuming 5 is an int, it's not possible to tell from the error message).

Anyone know what's going on here?

Thanks

David

+1  A: 

Okay, so I found out from this http://stackoverflow.com/questions/439003/how-do-you-map-an-enum-as-an-int-value-with-fluent-nhibernate that FluentNHibernate by default assumes that it's the string form of your enum that's saved in the database, not the int form.

Mental!

The simplest way around this is to modify the property's mapping as follows:

Map(x => x.WhateverThePropertyNameIs).CustomType(typeof(Int32));

Obviously, if your Enum is a byte rather than an int, specify that instead, etc etc.

The page I have linked to also describes how to specify that Fluent NHibernate should always treat enums as ints (or bytes or whatever).

Thanks

David

David