map an enum to int value

48 views
Skip to first unread message

cconway

unread,
Nov 5, 2009, 1:16:25 PM11/5/09
to Fluent NHibernate
Hello,
I'm having some issues trying to map an enum property using it's
integer value as opposed to the string value. Her's what I've got and
hopefully someone can tell me what I'm doing wrong. I'm using 1.0RTM
by the way.

First, I've got a class call EnumConvention that looks like this:
public class EnumConvention : IUserTypeConvention
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Property.PropertyType.IsEnum);
}
public void Apply(IPropertyInstance instance)
{
instance.CustomType(instance.Property.PropertyType);
}
}

In my mapping file, I've got the following:
Map(x => x.QueueType, "queueTypeID").CustomType(typeof
(EnumConvention));

The hbm that is generated looks like this:
<property name="QueueType" type="RMS.Data.Conventions.EnumConvention,
RMS.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<column name="queueTypeID" />
</property>

NHibernate.MappingException: Could not determine type for:
TotalVisibility.RMS.Data.Helpers.EnumConvention,
TotalVisibility.RMS.Data, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null, for columns: NHibernate.Mapping.Column
(queueTypeID)


Am I missing something? I thought I followed all the steps.

Chris C

unread,
Nov 7, 2009, 11:11:52 AM11/7/09
to Fluent NHibernate
You're problem is in the mapping file. A convention won't overwrite a
mapping if there is one, this makes it easy in a convention to set
defaults if a value hasn't been provided by the mapping but without
having to ensure you're not overwriting an existing mapping.

In you're mapping itself you're telling it the custom type for your
property is your "EnumConvention" hence the convention won't be able
to change that. Secondly, conventions are automatically run against
all the mappings in the project so you don't have to tell it to apply
the convention.

So in you're mapping it should just read:
Map(x => x.QueueType, "queueTypeID"); //Custom convention will set
the CustomType

Or,
Map(x => x.QueueType, "queueTypeID").CustomType(typeof
(QueueTypeEnum)); //Set explicitly, convention won't change this

Mohamed Meligy

unread,
Nov 8, 2009, 4:46:46 PM11/8/09
to fluent-n...@googlegroups.com
Hey,

May this reach the wiki or so? I think it's very important.

I also have another question: What if it was a string column instead of int?, where the value of the column is myEnumValue.ToString() ad the value of the enum comes from  (EnumType) Enum.Parse( typeof(EnumType), ColumnValue)... how would that be mapped ?




Regards,

--
Mohamed Meligy
Senior Developer, Team Lead Backup (.Net Technologies) – Applications Delivery - TDG
Injazat Data Systems
P.O. Box: 8230 Abu Dhabi, UAE.

Phone:  +971 2 6992700
Direct:   +971 2 4045385
Mobile:  +971 50 2623624, +971 55 2017 621

E-mail: eng.m...@gmail.com
Weblog: http://weblogs.asp.net/meligy

Chris C

unread,
Nov 8, 2009, 6:50:08 PM11/8/09
to Fluent NHibernate
NHibernate already provides a custom type for that, EnumStringType.
Check out the classes in NHibernate.Types.

On Nov 8, 9:46 pm, Mohamed Meligy <eng.mel...@gmail.com> wrote:
> Hey,
>
> May this reach the wiki or so? I think it's very important.
>
> I also have another question: What if it was a string column instead of
> int?, where the value of the column is myEnumValue.ToString() ad the value
> of the enum comes from  (EnumType) Enum.Parse( typeof(EnumType),
> ColumnValue)... how would that be mapped ?
>
> Regards,
>
> --
> Mohamed Meligy
> Senior Developer, Team Lead Backup (.Net Technologies) – Applications
> Delivery - TDG
> Injazat Data Systems
> P.O. Box: 8230 Abu Dhabi, UAE.
>
> Phone:  +971 2 6992700
> Direct:   +971 2 4045385
> Mobile:  +971 50 2623624, +971 55 2017 621
>
> E-mail: eng.mel...@gmail.com

Mohamed Meligy

unread,
Nov 9, 2009, 7:53:45 AM11/9/09
to fluent-n...@googlegroups.com
So, I can just use something like:

Map( x=> x.MyEnumProperty);

Or use the

Map(x => x.MyEnumProperty).CustomType(typeof(EnumStringType));

Or something else ?


Thank you.


Regards,

--
Mohamed Meligy
Senior Developer, Team Lead Backup (.Net Technologies) – Applications Delivery - TDG
Injazat Data Systems
P.O. Box: 8230 Abu Dhabi, UAE.

Phone:  +971 2 6992700
Direct:   +971 2 4045385
Mobile:  +971 50 2623624, +971 55 2017 621

cconway

unread,
Nov 9, 2009, 9:40:40 AM11/9/09
to Fluent NHibernate
Thanks for the clarification Chris!
Reply all
Reply to author
Forward
0 new messages