流利NHibernate的地理空间点映射 [英] Geospatial Point Mapping in Fluent NHibernate
问题描述
Place
类中存储一个地理点,但是当我运行我的ASP.NET MVC应用程序时,我总是收到一个NHibernate配置错误: 类型为'NHibernate.Spatial.Type.GeometryType'的方法'SetParameterValues'从
程序集'NHibernate.Spatial,Version = 1.0.0.0,Culture =中立,
PublicKeyToken = null'没有实现。
更新:这是由过时的NHibernate.Spatial DLL 。引用最新版本(2.2 +)解决了这个问题。感谢psousa为我带来解决方案。
$ b
地点
class:
使用System;
使用GisSharpBlog.NetTopologySuite.Geometries;
使用NHibernate.Validator.Constraints;
namespace MyApp.Data.Entities
{
public class Place
{
public virtual Guid Id {get;组; }
public virtual string Name {get;组; }
public virtual Point Location {get;组; }
$ h $ F code $> code> Mapping:
使用MyApp.Data.Entities;
使用FluentNHibernate.Mapping;
使用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));
$ b 流利的NHibernate配置 h2>
var cfg =流利.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => ; c.FromConnectionStringWithKey(connectionStringKey))
.ShowSql()
.Dialect(NHibernate.Spatial.Dialect.MsSql2008GeographyDialect,NHibernate.Spatial.MsSql2008))
.ExposeConfiguration(BuildSchema)$ b).Mappings(x => x.FluentMappings.AddFromAssembly(typeof(UserMap).Assembly)
.Conventions.AddFromAssemblyOf< ColumnNullabilityConvention>());
解决方案在您的映射上使用几何的CustomType。您应该使用地理的自定义类型。例如:
public class PlaceMap:ClassMap< Place>
{
public PlaceMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Location).CustomType(typeof(MsSql2008GeographyType)); // for for SQL2008
code $ $ $ $ $ $ $ $
$ p另外还有一些其他的你可能需要这样做。如果您的空间列的SRID不是0(零),并且您想要跳过NH xml映射,则需要声明一个自定义类型,如下所示: public class Wgs84GeographyType:MsSql2008GeographyType
{
protected override void SetDefaultSRID(GeoAPI.Geometries.Geometry geometry)
{
geometry.SRID = 4326 ;
$ b然后在你的映射上使用它: public class PlaceMap:ClassMap< Place>
{
public PlaceMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Location).CustomType(typeof(Wgs84GeographyType));
更新: p>
您应该引用NHibernate.Spatial.MsSql2008.dll,我建议您在数据库配置中使用强类型的Dialect方法。
.Dialect< MsSql2008GeographyDialect>()
I'm struggling to get Fluent NHibernate to play nice with SQL Server's Geospatial types. I want to store a geographic point in my Place
class, but I keep getting an NHibernate configuration error when I run my ASP.NET MVC app:
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.
Update: This is caused by an out-dated NHibernate.Spatial DLL. Referencing the latest version (2.2+) solves the problem. Kudos to psousa for leading me to a solution.
Place
class:
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; }
}
}
Fluent Place
Mapping:
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));
}
}
}
Fluent NHibernate Configuration:
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>());
解决方案 You're using a Geography dialect but using a CustomType of Geometry on your mapping. You should use a custom type of Geography. Something like:
public class PlaceMap : ClassMap<Place>
{
public PlaceMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Location).CustomType(typeof(MsSql2008GeographyType)); //for SQL2008
}
}
Also, there's something else that you may need to do. If your spatial column has an SRID different from 0 (zero), and if you want to skip NH xml mappings, you'll need to declare a custom type like this:
public class Wgs84GeographyType : MsSql2008GeographyType
{
protected override void SetDefaultSRID(GeoAPI.Geometries.IGeometry geometry)
{
geometry.SRID = 4326;
}
}
And then use it on your mapping:
public class PlaceMap : ClassMap<Place>
{
public PlaceMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Location).CustomType(typeof(Wgs84GeographyType));
}
}
UPDATE:
You should be referencing NHibernate.Spatial.MsSql2008.dll, and I would advise you to use the strongly-typed Dialect method in your database configuration.
.Dialect<MsSql2008GeographyDialect>()
这篇关于流利NHibernate的地理空间点映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!