Ok, it seems that you have to override SqlType when you're using a custom database type. You can just write a generic class inheriting from EnumStringType<T> and overriding SqlType and apply that one. See my updated answer on stackoverflow.
On Mon, Jun 7, 2010 at 8:35 AM, Mike Christensen <m...@kitchenpc.com> wrote: > I don't believe I explained the situation clear enough. The possible > values for the enum never change. It always must be one of those > values. However, which value it is can change. I have an enum called > OrderState which can be Preview, InQueue, or Completed. It can never > be anything else. I then have a column in my table that is of type > OrderState. I map my ActiveRecord class to my C# enum that has the > same values. I can then modify the State property of the class and it > will update the column in the database. > > Basically I'm mapping a C# enum to a database enum. This is a bit > tricky because usually you map a C# enum to either an integer > (default) or a string (EnumStringType) in the database. I didn't like > that solution because neither are very type safe. I also like to be > able to look at my data directly in SQL and see my enum values in > English rather than numbers that I forget what they mean. > > Mike > > On Mon, Jun 7, 2010 at 4:05 AM, Michael Sync <mchls...@gmail.com> wrote: > > Yes. It will be a pain.. If those enums are never change then we dont > need > > to put them in database. I was in situation where we want to allow user > to > > add those values. For example: We have the project statuses (such as Not > > Started, Started, Completed). We stored those values in database. User > > should be able to enter new value (Let's say "Delayed") in system as > well. > > But the problem was that we already need to modify the code when they > want > > to add new enum (or lockup) values. > > That's why Im just asking whether you guys faced the same problem in your > > project or not and love to know how you guys solved it. :) > > Thanks and Best Regards, > > Michael Sync > > > > Don't go the way life takes you. > > Take life the way you go > > > > http://michaelsync.net > > > > > > On Mon, Jun 7, 2010 at 6:59 PM, Mike Christensen <m...@kitchenpc.com> > wrote: > >> > >> These enums will never change. There's no reason to add more values > >> here (and if I ever do, I realize it'll be somewhat of a pain).. > >> > >> On Sun, Jun 6, 2010 at 9:08 PM, Michael Sync <mchls...@gmail.com> > wrote: > >> > I just check the Mauricio's answer on Stackoverflow. > >> > I have some confusions.. > >> >>>public enum OrderState {InQueue, Ordered, Error, Cancelled} > >> > If we add one more row (one more OrderState) in Database, Is the enum > >> > "OrderState" going to change automatically? > >> > > >> > The reason that we want to keep the enum in database is to allow user > to > >> > have as many value as we want, right? The code shouldn't be needed to > >> > re-compile.. > >> > > >> > On Mon, Jun 7, 2010 at 12:02 PM, Mauricio Scheffer > >> > <mauricioschef...@gmail.com> wrote: > >> >> > >> >> Yep, that's pretty much what I answered on stackoverflow ( > >> >> > >> >> > >> >> > http://stackoverflow.com/questions/2979026/mapping-enums-to-database-with-nhibernate-castle-activerecord > >> >> , I just see it here). Is it really necessary to override the SqlType > >> >> property? > >> >> > >> >> On Jun 5, 3:09 am, Mike Christensen <m...@kitchenpc.com> wrote: > >> >> > Update: > >> >> > > >> >> > Ok I found a kinda cleverish/hacky way of doing this. First off, I > >> >> > only have one mapping class: > >> >> > > >> >> > public class PgEnumMapper<T> : NHibernate.Type.EnumStringType<T> > >> >> > { > >> >> > public override NHibernate.SqlTypes.SqlType SqlType > >> >> > { > >> >> > get { return new NHibernate.SqlTypes.SqlType(DbType.Object); > } > >> >> > } > >> >> > > >> >> > } > >> >> > > >> >> > Then on my property, I setup the mapping like so: > >> >> > > >> >> > [Property(NotNull = true, SqlType = "OrderState", ColumnType = > >> >> > "DB.PgEnumMapper`1[DB.OrderState, WebSite], WebSite")] > >> >> > public OrderState State > >> >> > { > >> >> > get { return state; } > >> >> > set { state = value; } > >> >> > > >> >> > } > >> >> > > >> >> > If I find nothing better, I'll go this route.. I don't like it > 100% > >> >> > because it looks kinda hacky, and also if I change enum names, > Visual > >> >> > Studio cannot refactor them since they're all in strings.. Am I > >> >> > going > >> >> > down the right path, or is there some stupidly easy approach I'm > >> >> > missing? Thanks! > >> >> > > >> >> > Mike > >> >> > > >> >> > On Jun 4, 10:47 pm, Mike Christensen <m...@kitchenpc.com> wrote: > >> >> > > >> >> > > >> >> > > >> >> > > By default, an enum property gets mapped to an integer column > type > >> >> > > in > >> >> > > the database. I'm interested in mapping enums to an enum > database > >> >> > > type, such as this one: > >> >> > > >> >> > > CREATE TYPE OrderState AS ENUM ('Preview', 'InQueue', 'Ordered', > >> >> > > 'Error', 'Cancelled'); > >> >> > > >> >> > > It seems you can fairly easily change ActiveRecord to use strings > >> >> > > for > >> >> > > enums instead of integers, and there's several blog posts I've > >> >> > > found > >> >> > > on this. However, they have one big flaw. The SQL that gets > >> >> > > generated looks something like this: > >> >> > > >> >> > > INSERT INTO TestTable (State, Id) VALUES ('Preview'::text, > >> >> > > '9dea2a34-566a-45ea-84fd-24b86403ef5b'::uuid) > >> >> > > >> >> > > As you can see, the State value is casted to a text, which will > >> >> > > throw > >> >> > > a SQL error as the column type is OrderState. I've found one way > >> >> > > to > >> >> > > fix this, but I don't like my solution. I basically create an > >> >> > > IType > >> >> > > that knows how to map this enum: > >> >> > > >> >> > > public class OrderStateMapper : > >> >> > > NHibernate.Type.EnumStringType<OrderState> > >> >> > > { > >> >> > > public override NHibernate.SqlTypes.SqlType SqlType > >> >> > > { > >> >> > > get { return new > NHibernate.SqlTypes.SqlType(DbType.Object); > >> >> > > } > >> >> > > } > >> >> > > >> >> > > } > >> >> > > >> >> > > Since I override SqlType and return DbType.Object, NHibernate > will > >> >> > > no > >> >> > > longer try to cast my value into a text and everything will work. > >> >> > > The > >> >> > > reason I don't like this solution is I now have to create a > Mapper > >> >> > > class for every enum I want to map to my database, which in my > case > >> >> > > is > >> >> > > a lot and I want to be able to easily add more and not worry > about > >> >> > > this issue. I can see why EnumStringType needs to know the type > of > >> >> > > the enum, so it can parse data values and rehydrate OrderState > >> >> > > objects. However, isn't there a way to tell ActiveRecord to just > >> >> > > use > >> >> > > the string value for the enum and not try to cast it to text? > >> >> > > Basically the SQL statement I want to generate is: > >> >> > > >> >> > > INSERT INTO TestTable (State, Id) VALUES ('Preview', > >> >> > > '9dea2a34-566a-45ea-84fd-24b86403ef5b'::uuid) > >> >> > > or even > >> >> > > INSERT INTO TestTable (State, Id) VALUES ('Preview'::OrderState, > >> >> > > '9dea2a34-566a-45ea-84fd-24b86403ef5b'::uuid) > >> >> > > >> >> > > Thanks!! > >> >> > > >> >> > > Mike > >> >> > >> >> -- > >> >> You received this message because you are subscribed to the Google > >> >> Groups > >> >> "Castle Project Users" group. > >> >> To post to this group, send email to > >> >> castle-project-us...@googlegroups.com. > >> >> To unsubscribe from this group, send email to > >> >> castle-project-users+unsubscr...@googlegroups.com<castle-project-users%2bunsubscr...@googlegroups.com> > . > >> >> For more options, visit this group at > >> >> http://groups.google.com/group/castle-project-users?hl=en. > >> >> > >> > > >> > -- > >> > You received this message because you are subscribed to the Google > >> > Groups > >> > "Castle Project Users" group. > >> > To post to this group, send email to > >> > castle-project-us...@googlegroups.com. > >> > To unsubscribe from this group, send email to > >> > castle-project-users+unsubscr...@googlegroups.com<castle-project-users%2bunsubscr...@googlegroups.com> > . > >> > For more options, visit this group at > >> > http://groups.google.com/group/castle-project-users?hl=en. > >> > > >> > >> -- > >> You received this message because you are subscribed to the Google > Groups > >> "Castle Project Users" group. > >> To post to this group, send email to > >> castle-project-us...@googlegroups.com. > >> To unsubscribe from this group, send email to > >> castle-project-users+unsubscr...@googlegroups.com<castle-project-users%2bunsubscr...@googlegroups.com> > . > >> For more options, visit this group at > >> http://groups.google.com/group/castle-project-users?hl=en. > >> > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Castle Project Users" group. > > To post to this group, send email to > castle-project-us...@googlegroups.com. > > To unsubscribe from this group, send email to > > castle-project-users+unsubscr...@googlegroups.com<castle-project-users%2bunsubscr...@googlegroups.com> > . > > For more options, visit this group at > > http://groups.google.com/group/castle-project-users?hl=en. > > > > -- > You received this message because you are subscribed to the Google Groups > "Castle Project Users" group. > To post to this group, send email to castle-project-users@googlegroups.com > . > To unsubscribe from this group, send email to > castle-project-users+unsubscr...@googlegroups.com<castle-project-users%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/castle-project-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Castle Project Users" group. To post to this group, send email to castle-project-us...@googlegroups.com. To unsubscribe from this group, send email to castle-project-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.