Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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> {    ImportType<GisSharpBlog.NetTopologySuite.Geometries.Point>();    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()    {       Driver<SqlClientDriver>();    }     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     .AddFromAssemblyOf<LocationLog>() ); 

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 Microsoft.SqlServer.Types.SqlGeography.set_Srid(Int32 value) at Microsoft.SqlServer.Types.SqlGeography.Read(BinaryReader r) at SqlGeography::.DeserializeValidate(IntPtr , Int32 , CClrLobContext* )

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

  • http://nhibernate.info/doc/spatial/configuration-and-mapping.html
  • http://nhibernate.info/doc/spatial/sample-usage.html

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.

like image 417
Luke Foust Avatar asked Sep 29 '09 21:09

Luke Foust


1 Answers

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).

like image 83
Iain Avatar answered Jan 04 '23 14:01

Iain