Kevin Pang
Kevin Pang

Reputation: 41442

How do you map enums to and from the database using NHibernate?

Edit: Ryan raised a good point. I specifically want to be able to map to and from while still storing human-readable values in the database. That is, I don't want a bunch of enumeration integers in my database.

Upvotes: 1

Views: 2701

Answers (4)

kͩeͣmͮpͥ ͩ
kͩeͣmͮpͥ ͩ

Reputation: 7846

I think you can just set the type to string:

<property name="EnumProperty" Type="string" Length="50" NotNull="true" />

Upvotes: 0

Jasper
Jasper

Reputation: 846

According to the documentation you can either leave the type attribute of the property in your mapping file blank or you define it and specify the class name of the enumeration.

Another way would be to convert the enumeration to an int and use the int as the mapped type.

Upvotes: 5

Max Stewart
Max Stewart

Reputation: 3583

You have to implement a custom IUserType. See this post.

Upvotes: 2

Ryan
Ryan

Reputation: 9928

I've never used NHibernate, but can't you just set the SQL datatype to int?

Upvotes: 0

Related Questions