views:

114

answers:

1

I have an object with a property that I'd like to map as Serializable. NHibernate supports this:

<property name="FeeGenerator" column="FeeGenerator" type="Serializable"  />

Is there a way to accomplish this in Fluent NHibernate?

There's an SO question (http://stackoverflow.com/questions/2000798/map-to-serializable-in-fluent-nhibernate) that would seem to address this, but the only response there doesn't work for me.

If I set

CustomType<NHibernate.Type.SerializableType>();

I get the following Exception:

Could not instantiate IType SerializableType: System.MissingMethodException: No parameterless constructor defined for this object.
+1  A: 

I'm surprised that nobody seems to know the answer to this. I did find the answer, and I figured I'd share it.

Basically, you can use the CustomType method to map to any NHibernate type just like you would in XML because there's an overload that takes a string. So the following

CustomType("Serializable");

outputs

<property name="PropertyName" type="Serializable"> ...

I also blogged about this (with some background and more details) over at http://blog.statichippo.com/archive/2010/01/20/mapping-serializable-types-using-fluent-nhibernate.aspx

statichippo
How about serializing to Xml rather than VARBINARY?
row1