本文介绍了流利的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

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

解决方案

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几何类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:14
查看更多