Mapping enums to int NOT string

441 views
Skip to first unread message

Dick Walker

unread,
Sep 23, 2009, 3:39:47 AM9/23/09
to Fluent NHibernate
Hi,

we use enums in several places and I'd much rather not be storing
these as strings. NHibernate uses ints, but Fluent forces it to a
string. We are declaring our enums like this:
public enum Status
{
InProgress = 0,
Succeeded = 1,
Failed = 2
}
so even if someone adds a new element, the existing values are
maintained.

My question is, how can I get Fluent to revert to ints??? It looks I
need a convention, perhaps, but the examples I have found seem to be
out of date. It seems like the IPropertyConvention interface has
changed.

Was:
public class CustomEnumerationTypeConvention : IPropertyConvention
{
public bool Accept(IProperty property)
{
return true;
}

public void Apply(IProperty property)
{
property.
}
}

Is now:
public class CustomEnumTypeConvention : IPropertyConvention
{
public void Apply
(FluentNHibernate.Conventions.Instances.IPropertyInstance instance)
{
instance.
}
}


Any suggestions would be greatly appreciated :)

cheers, Dick

Bogdan Ungureanu

unread,
Sep 23, 2009, 3:47:46 AM9/23/09
to fluent-n...@googlegroups.com
Please take a look here http://stackoverflow.com/questions/1345426?sort=newest#sort-top .It's my solution.

2009/9/23 Dick Walker <di...@granitesolutions.com.au>



--
Bogdan Ungureanu 0746.090.664

Mikael Henriksson

unread,
Sep 23, 2009, 5:30:11 AM9/23/09
to fluent-n...@googlegroups.com
All you need is a convention and then add the convention to you fluent mappings.

public class EnumConvention :
    IPropertyConvention, 
    IPropertyConventionAcceptance
{
    #region IPropertyConvention Members

    public void Apply(IPropertyInstance instance)
    {
        instance.CustomType(instance.Property.PropertyType);
    }

    #endregion

    #region IPropertyConventionAcceptance Members

    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(x => x.Property.PropertyType.IsEnum);
    }

    #endregion
}

Dick Walker

unread,
Sep 24, 2009, 12:22:22 AM9/24/09
to Fluent NHibernate
Hi Mikael,

I tried your approach, but it still creates the DB field as a string
and writes the enum item names into the column :(

I have another convention loaded which is working fine (no underscore
in FK column names). I have set a break point in the apply method and
seems to run the code.

Do you change the mapping code at all?? I am using the default "Map(x
=> x.Status);" Do I need to do something different there.

cheers, Dick

On Sep 23, 7:30 pm, Mikael Henriksson <mik...@zoolutions.se> wrote:
> All you need is a convention and then add the convention to you fluent
> mappings.
>
> public class EnumConvention :
>     IPropertyConvention,
>     IPropertyConventionAcceptance
> {
>     #region IPropertyConvention Members
>
>     public void Apply(IPropertyInstance instance)
>     {
>         instance.CustomType(instance.Property.PropertyType);
>     }
>
>     #endregion
>
>     #region IPropertyConventionAcceptance Members
>
>     public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
>     {
>         criteria.Expect(x => x.Property.PropertyType.IsEnum);
>     }
>
>     #endregion
>
> }
>
> On Wed, Sep 23, 2009 at 9:47 AM, Bogdan Ungureanu <daemo...@gmail.com>wrote:
>
> > Please take a look here
> >http://stackoverflow.com/questions/1345426?sort=newest#sort-top.It's my
> > solution.
>
> > 2009/9/23 Dick Walker <d...@granitesolutions.com.au>

Dick Walker

unread,
Sep 24, 2009, 12:47:05 AM9/24/09
to Fluent NHibernate
Hi Bogdan,

thanks for that. The custom class solution worked. It seems like a lot
of work, and a convention should have done it, but it works!!! :)

Thank you :)

cheers, Dick

On Sep 23, 5:47 pm, Bogdan Ungureanu <daemo...@gmail.com> wrote:
> Please take a look herehttp://stackoverflow.com/questions/1345426?sort=newest#sort-top.It's my
> solution.
>
> 2009/9/23 Dick Walker <d...@granitesolutions.com.au>
Reply all
Reply to author
Forward
0 new messages