流畅NHibernate中的地理空间点映射

Pet*_*ron 1 asp.net-mvc spatial fluent-nhibernate sql-server-2008

我很难让Fluent NHibernate在SQL Server的地理空间类型中发挥出色.我想在我的Place类中存储一个地理点,但是当我运行ASP.NET MVC应用程序时,我不断收到NHibernate配置错误:

Method 'SetParameterValues' in type 'NHibernate.Spatial.Type.GeometryType' from
assembly 'NHibernate.Spatial, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null' does not have an implementation.
Run Code Online (Sandbox Code Playgroud)

更新:这是由过时的NHibernate.Spatial DLL引起的.引用最新版本(2.2+)可以解决问题.感谢psousa带领我找到解决方案.

Place 类:

using System;
using GisSharpBlog.NetTopologySuite.Geometries;
using NHibernate.Validator.Constraints;

namespace MyApp.Data.Entities
{
    public class Place
    {
        public virtual Guid Id { get; set; }
        public virtual string Name { get; set; }
        public virtual Point Location { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

流利的Place映射:

using MyApp.Data.Entities;
using FluentNHibernate.Mapping;
using NHibernate.Spatial.Type;

namespace MyApp.Data.Mappings
{
    public class PlaceMap : ClassMap<Place>
    {
        public PlaceMap()
        {
            ImportType<GisSharpBlog.NetTopologySuite.Geometries.Point>();

            Id(x => x.Id);
            Map(x => x.Name);

            Map(x => x.Location)
                .CustomType(typeof(GeometryType));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

流畅的NHibernate配置:

var cfg = Fluently.Configure()
    .Database(MsSqlConfiguration.MsSql2008
        .ConnectionString(c => c.FromConnectionStringWithKey(connectionStringKey))
    .ShowSql()
    .Dialect("NHibernate.Spatial.Dialect.MsSql2008GeographyDialect,NHibernate.Spatial.MsSql2008"))
    .ExposeConfiguration(BuildSchema)
    .Mappings(x => x.FluentMappings.AddFromAssembly(typeof(UserMap).Assembly)
    .Conventions.AddFromAssemblyOf<ColumnNullabilityConvention>());
Run Code Online (Sandbox Code Playgroud)

pso*_*usa 5

您正在使用地理方言,但在地图上使用CustomType of Geometry.您应该使用自定义类型的地理位置.就像是:

public class PlaceMap : ClassMap<Place>
{
    public PlaceMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);

        Map(x => x.Location).CustomType(typeof(MsSql2008GeographyType)); //for SQL2008
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,您可能还需要做其他事情.如果空间列的SRID不等于0(零),并且如果要跳过NH xml映射,则需要声明如下自定义类型:

public class Wgs84GeographyType : MsSql2008GeographyType
{
    protected override void SetDefaultSRID(GeoAPI.Geometries.IGeometry geometry)
    {
        geometry.SRID = 4326;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在映射上使用它:

public class PlaceMap : ClassMap<Place>
{
    public PlaceMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);

        Map(x => x.Location).CustomType(typeof(Wgs84GeographyType));
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:

你应该引用NHibernate.Spatial.MsSql2008.dll,我建议你在数据库配置中使用强类型的Dialect方法.

.Dialect<MsSql2008GeographyDialect>()
Run Code Online (Sandbox Code Playgroud)

  • 感谢您提示覆盖默认SRID. (2认同)