流利的NHibernate自动映射PostGIS几何类型 [英] Fluent NHibernate automap PostGIS geometry type

查看:176
本文介绍了流利的NHibernate自动映射PostGIS几何类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下模型:使用NetTopologySuite.Geometries;

  

public class bounding_box
{
public virtual int id {get;保护组}
公共虚拟多边形区域{get;组; }

$ / code $


如何自动映射区域属性复制到区域几何(多边形)列中。请注意,我不在乎能够使用NHibernate读取/更新几何列,因为我将在代码中使用GDAL。



我知道我可以做到实现一个手动覆盖,即:

  public class bounding_boxMappingOverrride:IAutoMappingOverride< bounding_box> 
{
public void Override(AutoMapping< bounding_box> mapping)
{
mapping.Map(x => x.area)
.CustomSqlType多边形));






$ b

然而,我有许多带几何列的表,更希望能够指定一个自定义的类型映射。

由于某些原因,区域属性永远不会被以下属性约定截取:

  public class PostgisTypesConvention:IPropertyConvention 
{
public void应用(IPropertyInstance实例)
{
if(instance.Type == typeof(Polygon))
{
instance.CustomSqlType(geometry(Polygon)); //永远不会达到



code $ $ $ $ $ $ $ $ $ $ $如果我使用 GeoAPI.Geometries.IPolygon 而不是 NetTopologySuite.Geometries.Polygon ...

解决方案

我终于可以通过定义一个自定义 UserTypeConvention ,即:

 使用NetTopologySuite.Geometries; 
使用NHibernate.Spatial.Type;

public class PostGisPolygonUserTypeConvention:UserTypeConvention< PostGisGeometryType>
{
public override void Accept(IAcceptanceCriteria< IPropertyInspector> criteria)
{
criteria.Expect(c => c.Type == typeof(Polygon));
}

public override void Apply(IPropertyInstance instance)
{
//必须将CustomType设置为可以使用NHibernate读取/写入行
instance.CustomType< PostGisGeometryType>();
//必须设置CustomSqlType来生成正确的SQL模式
instance.CustomSqlType(geometry(Polygon));






$同样的原理也可以用来创建<$对于其他几何图形,例如 Point LineString UserTypeConventions c $ c> MultiPoint
等。


Given the following model:

using NetTopologySuite.Geometries;

public class bounding_box
{
    public virtual int id { get; protected set; }
    public virtual Polygon area { get; set; }
}

How do I automap the area property to a area geometry(Polygon) column when generating the DB schema using Fluent Nhibernate? Note that I do not care about being able to read / update the geometry column using NHibernate since I will be using GDAL in my code.

I know I can do it by implementing a manual override, i.e.:

public class bounding_boxMappingOverrride : IAutoMappingOverride<bounding_box>
{
    public void Override(AutoMapping<bounding_box> mapping)
    {
        mapping.Map(x => x.area)
            .CustomSqlType("geometry(Polygon)");
    }
}

However, I have many tables with geometry columns so I would much prefer to be able to specify a custom type mapping.

For some reason, the area property is never intercepted by the following property convention:

public class PostgisTypesConvention : IPropertyConvention
{
    public void Apply(IPropertyInstance instance)
    {
        if (instance.Type == typeof(Polygon))
        {
            instance.CustomSqlType("geometry(Polygon)"); // Never reached
        }
    }
}

I have the same problem if I use GeoAPI.Geometries.IPolygon instead of NetTopologySuite.Geometries.Polygon...

解决方案

I was finally able to resolve this by defining a custom UserTypeConvention, i.e.:

using NetTopologySuite.Geometries;
using NHibernate.Spatial.Type;

public class PostGisPolygonUserTypeConvention : UserTypeConvention<PostGisGeometryType>
{
    public override void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(c => c.Type == typeof(Polygon));
    }

    public override void Apply(IPropertyInstance instance)
    {
        // Have to set CustomType to be able to read/write rows using NHibernate
        instance.CustomType<PostGisGeometryType>();
        // Have to set CustomSqlType to generate correct SQL schema
        instance.CustomSqlType("geometry(Polygon)");
    }
}

The same principle can also be used to create UserTypeConventions for other geometries, such as Point, LineString, MultiPoint, etc.

这篇关于流利的NHibernate自动映射PostGIS几何类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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