Null Geometry values in NHibernate.Spatial for MsSql2008

I'm currently trying to use NHibernate.Spatial with SQL Server 2008 (which natively supports Geometry and Geography types and indices) and so far everything works (thanks to the really good wikis on NHibernate Forge).

Unfortunately the MsSql2008 dialect implemented in NHibernate.Spatial has a known bug: in case you decide to store a NULL value in a Geometry column, the SQL generated by NHibernate uses a completely wrong parameter type (using tinyint instead of the intended geometry).

To solve the problem you need to download the NHibernate.Spatial code via SourceForge and open up the \src\NHibernate.Spatial.MsSql2008\Type\MsSql2008GeometryType.cs file. At line 58 or so you'll find:

/// <summary>
/// Converts from GeoAPI geometry type to database geometry type.
/// </summary>
/// <param name="value">The GeoAPI geometry value.</param>
/// <returns></returns>
protected override SqlGeometry FromGeometry(object value)
{
    IGeometry geometry = value as IGeometry;
    if (geometry == null)
    {
        return null;
    }
    //...

As you can see, when the value you are trying to assign to a Geometry column is null, the MsSql2008GeometryType simply passes null to the component that actually writes data to the SQL query. You need to correct that line passing a valid NULL value back:

protected override SqlGeometry FromGeometry(object value)
{
    IGeometry geometry = value as IGeometry;
    if (geometry == null)
    {
        return SqlGeometry.Null;
    }
    //...

Compile everything and use the newly generated NHibernate.Spatial.MsSql2008.dll library to store NULL values correctly.  ;)