NHibernate.Spatial 和 Sql 2008 地理类型 - 如何配置 [英] NHibernate.Spatial and Sql 2008 Geography type - How to configure

查看:22
本文介绍了NHibernate.Spatial 和 Sql 2008 地理类型 - 如何配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Nhibernate 与 Sql 2008 Geography 类型一起使用,但遇到了困难.我正在使用 Fluent Nhibernate 来配置我对它很陌生的配置,所以这也可能是问题所在.

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;}
}

映射类如下所示:

public class LocationLogMap : ClassMap<LocationLog>
{
   ImportType<GisSharpBlog.NetTopologySuite.Geometries.Point>();
   Id(x => x.Id);
   Map(x => x.TimeStamp).Generated.Insert();
   Map(x => x.Location);
}

为了在 Fluent Nhibernate 中使用 MsSql2008GeographyDialect,我创建了自己的配置类:

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>()
);

所有这些都是为了设置我在尝试将 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:

一个 .NET Framework 错误发生在执行用户定义的程序或聚合地理":System.ArgumentException:24204:空间参考标识符 (SRID) 是无效.指定的 SRID 必须匹配支持的 SRID 之一显示在sys.spatial_reference_systems 目录看法.System.ArgumentException: 在Microsoft.SqlServer.Types.SqlGeography.set_Srid(Int32值)在Microsoft.SqlServer.Types.SqlGeography.Read(BinaryReader鼠SqlGeography::.DeserializeValidate(IntPtr, Int32 , CClrLobContext* )

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

我已阅读以下有关如何配置和使用 Nhibernate Spatial 库的文章:

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

但似乎都没有帮助.任何有配置 Nhibernate 以使用空间地理类型并能提供任何见解的经验的人都将不胜感激.

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.

推荐答案

我也是如此,多亏你的开始,我才开始工作(插入和读取空间数据).对于其他感兴趣的人,首先 GisSharpBlog.NetTopologySuite.Geometries.Point 类位于 NetTopologySuite.dll 中,它是 nHibernate.Spatial 下载的一部分.

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.

其次,根据 James 的观点,确保将 SRID 设置为 4326.

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

我正在使用 Geography,但我在某处读到使用 GeometryType 可能有用并且对我有用(我插入了一些点并在数据库中验证了它).我还读到最好为 Geography 编写 SQL 查询,以便您可以使用特殊的 SQL 2008 Spatial 方法(而不是使用 Criteria).

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

这篇关于NHibernate.Spatial 和 Sql 2008 地理类型 - 如何配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆