Normalizing EnumStringType in NHibernate

I am currently using an enumeration in NHibernate, displayed 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 fine, except that the database is far from normal, each row in the People table has a column containing one of four text rows. Now I have split this into a new table, but I'm not sure how to create a mapping in NHibernate. My first instinct was just <many-to-one> , but I do not want this listing to be his own essence. I just need NHibernate to do a simple join to bind to an extra column.

As a stop gap, I just created a view that works great, but I would prefer the abstraction layer to be in my NHibernate map rather than in the schema.

+3
c # nhibernate nhibernate-mapping
source share
1 answer

If I understand correctly, your database normalizes perfectly. You said that NHibernate stores Enum as a string, which is good. Enumeration is not an entity. Its definition should not be stored in the database since it is defined in C #.

If you remove EnumStringType and draw enum as Int32, you will get it as a number. This may look more “normalized” to you, but it is a potential problem when you change your enumeration and need to cope with outdated databases.

+5
source share

All Articles