Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to persist an enum using NHibernate

Is there a way to persist an enum to the DB using NHibernate? That is have a table of both the code and the name of each value in the enum.

I want to keep the enum without an entity, but still have a foreign key (the int representation of the enum) from all other referencing entities to the enum's table.

like image 545
Meidan Alon Avatar asked Nov 02 '08 15:11

Meidan Alon


1 Answers

Why are you guys over complicating this? It is really simple.

The mapping looks like this:

<property name="OrganizationType"></property> 

The model property looks like this:

public virtual OrganizationTypes OrganizationType { get; set; } 

The Enum looks like this:

public enum OrganizationTypes {     NonProfit = 1,     ForProfit = 2 } 

NHibernate will automatically figure it all out. Why type more than you need????

like image 110
Emad Avatar answered Oct 12 '22 23:10

Emad