Search code examples
postgresqlnhibernatenhibernate-mappingspatial

NHibernate Postgres spatial geometry type become NULL


I am using postgres/postgis as db and Nhibernate as ORM on a C# project.

This is the first time I try to map postgis to a .Net application. My setup uses NHibernate version 4.0.4.4000 and NHibernate.Spatial.PostGis 4.0.4.4000 (with Npgsql version 3.0.5)

I followed this tutorial in order to set up the mappings, so I have:

in the web.config

<property name="dialect">NHibernate.Spatial.Dialect.PostGisDialect,NHibernate.Spatial.PostGis</property>

for the nhibernate configuration I added

cfg.AddAuxiliaryDatabaseObject(new SpatialAuxiliaryDatabaseObject(cfg));

my xml mapping file has the following mapping:

<property name="Geom" column="the_geom" type="NHibernate.Spatial.Type.GeometryType, NHibernate.Spatial" />

also tried using PostGis namespace in the mapping:

<property name="Geom" column="the_geom">
    <type name="NHibernate.Spatial.Type.PostGisGeometryType, NHibernate.Spatial.PostGis">
        <param name="srid">3006</param>
        <param name="subtype">POLYGON</param>
    </type>
</property> 

and my model class has the following property

public virtual IGeometry Geom { get; set; }

Now when ever I fetch any of the geometries the property Geom is null. In the logs I don't get exceptions nor error messages. I don't know what else to try.

In this question, using MS sql server, he got it working because he figured out that hist current dll (Microsoft.SqlServer.Types.dll) and the required one supported different versions of Ms sql, so mapping that dll to an older version fixed the problem. Could that be the case here? how/what can I try?

I have also looked at this post where it worked for him by defining his own UserTypeConvention but I don't have "UserTypeConvention" defined, so it does not work for me.

I have tried posting this question at gis stackexchange with no results, so I am trying here.


Solution

  • The issue here was that the version of Npgsql which I was using (version 3.0.5). There seems to be a problem with that version and does not handle Geometry correctly and so that is why I get null.

    Downgrade to version 2.2.7 of Npgsql and it works.

    A second problem I found was that I was not able to do any inserts to tables which have Identity columns, so I had to change the dialect, following this link it helped me.

    I also need to state that I could not have solved this if it was not by the help of rsquare who is the one which actually found this issue between nhibernte and version 3.x of Npgsql.