onsdag 30 januari 2008

NHibernate mapping with enum

When mapping a property of an enumeration type you should always set the type attribute of that property in the mapping file to that enumaration type.

Often you don't set the type at all or you set it to Int32 since your enum is represented as an int in the database. It's easy to assume that setting the attribute type to Int32 will have the same effect as using the enum type. And it will work!

When using an In32 your code will execute without exception. The field will be selected and updated as usual.
However, during the session flush nhibernate will recognize that the current value of the property differs from the originall value causing an update to the database. Another disadvantage is that the query cache for this class won't work either.

Inga kommentarer: