Apr
21
Written by:
Javier Callico
4/21/2011
I’m almost positive that everybody who has used NHibernate beyond a simple Hello World application has been bitten by this. Today it was my turn.
I was using a simple enumeration on one of my entities:
public enum ComparisonType
{
Equals = 1,
LessThan = 2,
LessThanOrEqual = 3,
GreaterThan = 4,
GreaterThanOrEqual = 5
}
… my initial entry in the mapping file for a property for this enumeration looked like this:
<property name="ComparisonType" not-null="true" type="Int32" column="ComparisonTypeId"></property>
I was able to load the entity without any problems and the int value coming from the database was correctly translated to the enumeration value.
Now when profiling the application I noticed that NHibernate was emitting an update statement for the entity even when no change was done in memory.
After spending quite some time trying to find the cause of the problem I found this article which pointed me in the right direction: Ghosting.
The solution?
Do not specify the type on the mapping file, let NHibernate figure it our on it’s own.
<property name="ComparisonType" not-null="true" column="ComparisonTypeId"></property>
References:
- NHibernate updates unchanged records
- How Test your mappings: the Ghostbuster
2 comment(s) so far...
Re: Beware when mapping enumerations using NHibernate
Dude, btw, did you try EF 4.1 ? What do you think comparing with NHibernate?
By Remus on
7/8/2011
|
Re: Beware when mapping enumerations using NHibernate
No I haven't used EF 4.1 in a "real" project yet... I'll try to post about my first time experience when compared with NHibernate as soon as I use it.
By Javier Callico on
7/8/2011
|