Fluent NHibernate 中的地理空间点映射

发布于 2024-11-08 14:14:17 字数 1898 浏览 3 评论 0原文

我正在努力让 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.

更新:这是由过时的 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; }
    }
}

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风月客 2024-11-15 14:14:17

您使用地理方言,但在地图上使用几何的自定义类型。您应该使用自定义类型的地理。例如:

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

        Map(x => x.Location).CustomType(typeof(MsSql2008GeographyType)); //for SQL2008
    }
}

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

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

然后在映射中使用它:

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

        Map(x => x.Location).CustomType(typeof(Wgs84GeographyType));
    }
}

UPDATE:< /strong>

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

.Dialect<MsSql2008GeographyDialect>()

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>()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文