views:

509

answers:

1

I've seen several questions related to properly mapping an enum type using NHibernate.

This article by Jeff Palermo showed me how to do that properly by creating a custom type. I use Schema Export to create my DB during my dev cycles, but this method breaks my export statement. Is there a way to specify the type of the column on export?

Here is my enum code:

public enum OperatorCode
{
    CodeA,
    CodeB,
    CodeC,
    CodeD
}

Here is my custom type:

public class OperatorCodeType:EnumStringType
{
    public OperatorCodeType():base(typeof(OperatorCode),20)
    {

    }
}

Here is my property in my mapping file:

<property name="OperatorCode" column="OperatorCode" type="OperatorCodeType"  />

And finally here is my class declaration for that property:

public virtual OperatorCode OperatorCode { get; set; }

Is it even possible to do this?

+1  A: 

I have not tested it, but you can use the Column declaration within a property to specify the sql type. Example from the docs:

<property name="Foo" type="String">
    <column name="foo" length="64" not-null="true" sql-type="text"/>
</property>

Granted this is a string, but you may want to try it with the type of OperatorCodeType, column sql-type as text or nvarchar or whatever works.

If you try it, let me know? Not near my dev machine at the moment.

Chad Ruppert
Chad,This worked like a charm. I've never split a property element out with a column node, but I thought there had to be some way to do it in the .hbm files, since I saw that Fluent NHibernate lets you do it through code. The column declaration gives more fine-grained control. Thanks a lot!
Mark Struzinski
Not a problem, glad I could help.
Chad Ruppert