Mapping PostgreSQL ENUM types to C# Enums

390 views
Skip to first unread message

Mike Christensen

unread,
Sep 17, 2009, 2:21:46 PM9/17/09
to Castle Project Users
In ActiveRecord, I can easily map a column to an enum..

enum enumColors {Green, Blue, Red};

[Property(NotNull = true)]
public enumColors Color
{
get { return color; }
set { color = value; }
}

In the DB, it creates an integer column type and green is stored as 0,
blue is stored as 1 and red is stored as 2. This is fine. However, I
want to take advantage of Postgres ENUM types:

CREATE TYPE colors AS ENUM ('Green', 'Blue', 'Red);
ALTER TABLE Foo ADD COLUMN Color colors;

Now I have a DB type called colors and a column in the table Foo
called Color which is of type colors. The question is how do I map my
class to this data type? ActiveRecord appears to want to read/write
the data as an integer, and Postgres returns it as a string. Any
ideas? Thanks!

Mike

Tyler Burd

unread,
Sep 17, 2009, 2:51:00 PM9/17/09
to castle-pro...@googlegroups.com
From memory, so it may be inaccurate:

NHibernate.Type.EnumStringType is the class you're interested in.

Usage:

[Property(ColumnType="NHibernate.Type.EnumStringType`1[[enumColors, YOUR_ASSEMBLY]], NHibernate")]
public enumColors Color
{
get { return color; }
set { color = value; }
}

Mauricio Scheffer

unread,
Sep 17, 2009, 3:29:24 PM9/17/09
to Castle Project Users
Reply all
Reply to author
Forward
0 new messages