My first implementation of this was to use a full grown entity class to represent gender. Then I would have a many-to-one relationship from e.g. Patient to Gender. The more I look at this, it doesn't feel right to me any more, as gender is something that's not likely change any soon. And as I'm using Guid comb for mapping the primary keys, rather lot of space is taken up for the foreign key and when looking at the raw database table, it's hard to figure out what the Guid represents. So, I'd like to know how other developers are tackling this and similar mappings (e.g colour). It's surely something is something used quite often.
Asked
Active
Viewed 194 times
0
-
Possible duplicate of [Is normalizing the gender table going too far?](http://stackoverflow.com/q/3513606/634872). – Florian Lim Mar 25 '11 at 22:12
-
@Florian Lim I have read this discussion before, but as far I can see it doesn't mention NHibernate at all. It's purely about database normalization. – zszep Mar 25 '11 at 22:20
-
You are right, it doesn't mention NHibernate. But the question "How do I map this?" is secondary. The primary question here is, "Is it enough for me to just a use a char(1) column or do I need an extra table? If you decide to have a separate table, the mappings are no different than for any other table/class like Department or any other many-to-one relationship. – Florian Lim Mar 25 '11 at 22:31
-
As I have mentioned in my question, I have mapped the gender as a full grown entity (and hence a separate table) up to now. It just doesn't feel right to me. So I'm just asking how people handle this situation in their software (with regards to NHibernate). – zszep Mar 25 '11 at 22:39
1 Answers
4
As they are properties which rarely change (without being too political, there are only 2 human genders), I map them as enum
.
public enum Genre
{
Unknown = 0,
Male = 1,
Female = 2
}
By default, this would save the string value to the database (i.e., "Unknown", "Male" or "Female"), so to get it to save the integer value (i.e., 0, 1 or 2), I map it as:
Map(x => x.Genre).CustomType(typeof(Genre));
Sometimes I find it better to save the string value instead of the integer, so I juggle between the two mappings accordingly.

Jamie Ide
- 48,427
- 16
- 81
- 117

rebelliard
- 9,592
- 6
- 47
- 80