views:

763

answers:

1

I am currently using an enumeration in an NHibernate mapped as follows..

public enum UploadMethod
{
    Java, Silverlight, Gears, Flash
}

class UploadMethodType : EnumStringType
{
    public UploadMethodType() : base(typeof(UploadMethod), 255) { }
}

public class Person
{
    /* Bunch of non interesting properties... */
    public UploadMethod PreferredUploadMethod { get; set; }
}

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
 <class name="Person" lazy="false" table="[dbo].[People]">
  <!-- Bunch of non interesting properties... -->
  <property name="PreferredUploadMethod" type="UploadMethodType" />
 </class>
</hibernate-mapping>

Which works perfectly except that the database is far from normalized, every row in the People table has a column containing one of four text strings. I've now separated this into a new table, but I'm not sure how to create the mapping in NHibernate. My first instinct was just <many-to-one> but I don't want this enumeration to be its own entity, I basically just need NHibernate to do a simple join to tack on the extra column.

As a stop gap I just created a view, which works fine, but I'd rather that abstraction layer be in my NHibernate mapping, not the schema.

+2  A: 

If I understand you right, your database is normalized perfectly. You told NHibernate to store the Enum as a string, which is a good thing. The enum is not an entity. Its definition should not be stored in the database, because it is defined in C#.

If you remove the EnumStringType and map the enum as Int32, you get it as a number. This may look more "normalized" for you, but is a potential problem when you change your enum and have to cope with legacy databases.

Stefan Steinegger