NHibernate knows when an object under its watch has changed. As soon as the object changes, it is “dirty”. Some other changes might cause an object to be dirty as well. One that my team recently encountered is a cast. We use an enum of type byte. It’s only a few items (less than 255), so we use a tinyint in our database. When our mapping uses type=”byte”, NHibernate casts from the byte to our Enum type when hydrating the object. This cast is a change because when NHibernate checks the value, it’s an Enum, not a byte.
To get around this cast (implicit or not), we use the fully qualified type name of the Enum in the mapping. NHibernate understands Enums natively, so just put in the enum type, and you are off to the races. Note that if you are using an Enum that is nested inside a public class, you need to follow .Net’s rules for fully-qualified type names.
See MSDN’s documentation for this:
http://msdn2.microsoft.com/en-us/library/w3f99sx1.aspx
TopNamespace.SubNameSpace.ContainingClass+NestedEnum,MyAssembly
I’ve tried it both ways, and when going from “byte” to my C# enum, my object is dirty right away. Specifying the enum in the mapping gives me the correct behavior. I’ll admin I didn’t try it with a normal Enum that defaults to the “Int32″ data type.
You can just drop the type name completely, NHibernate will do the Right Thing here.
Actually, this is the first time I hear about this issue, because I always leave the type name when it is obvious from the classes.