Luke Foust Luke Foust - 1 year ago 114
C# Question

NHibernate.Spatial and Sql 2008 Geography type - How to configure

I am trying to use Nhibernate with the Sql 2008 Geography type and am having difficulty. I am using Fluent Nhibernate to configure which I am fairly new to so that may be the problem as well.

First, the class I am trying to persist looks something like:

public class LocationLog : FluentNHibernate.Data.Entity
public virtual new int Id {get;set;}
public virtual DateTime TimeStamp {get;set;}
public virtual GisSharpBlog.NetTopologySuite.Geometries.Point Location {get;set;}

The mapping class looks like:

public class LocationLogMap : ClassMap<LocationLog>
Id(x => x.Id);
Map(x => x.TimeStamp).Generated.Insert();
Map(x => x.Location);

In order to use the MsSql2008GeographyDialect with Fluent Nhibernate, I have created my own configuration class:

public class Sql2008Configuration
: PersistenceConfiguration<Sql2008Configuration, MsSqlConnectionStringBuilder>
public Sql2008Configuration()

public static Sql2008Configuration MsSql2008
get { return new Sql2008Configuration().Dialect<MsSql2008GeographyDialect>(); }

so I have configuration code like:

var configuration = Fluently.Configure()
.Database(Sql2008Configuration.MsSql2008.ConnectionString(c => c.Is(connectionString)))
.Mappings(m => m.FluentMappings

All of this to setup the fact that I am getting the following error when trying to persist the LocationLog type to the database:

A .NET Framework error occurred during
execution of user-defined routine or
aggregate "geography":
System.ArgumentException: 24204: The
spatial reference identifier (SRID) is
not valid. The specified SRID must
match one of the supported SRIDs
displayed in the
sys.spatial_reference_systems catalog
view. System.ArgumentException: at
value) at
r) at
, Int32 , CClrLobContext* )

I have read the following articles about how to configure and use the Nhibernate Spatial libraries:

but neither seem to help. Anybody that has experience configuring Nhibernate to use the Spatial Geography types who could provide any insights would be greatly appreciated.

Answer Source

I am in the same boat, and thanks to your start I got it working (inserting and reading spatial data). For anyone else who is interested, firstly the GisSharpBlog.NetTopologySuite.Geometries.Point class is in NetTopologySuite.dll which is part of the nHibernate.Spatial download.

Secondly, as per James point, make sure you set the SRID to 4326.

And lastly, the map needs to look like this:

Map(a => a.Location).CustomType(typeof(NHibernate.Spatial.Type.GeometryType));

I am using Geography, but I read somewhere that using GeometryType may work and it does for me (I inserted some points and verified it in the database). I also read that its best to write SQL Query's for Geography so that you can use the special SQL 2008 Spatial methods (as opposed to using Criteria).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download