给定以下模型:
using NetTopologySuite.Geometries;
public class bounding_box
{
public virtual int id { get; protected set; }
public virtual Polygon area { get; set; }
}
使用Fluent Nhibernate生成数据库模式时,如何将
area
属性自动映射到area geometry(Polygon)
列?请注意,由于我将在代码中使用GDAL,因此我不关心能够使用NHibernate读取/更新几何列。我知道我可以通过实现手动替代来做到这一点,即:
public class bounding_boxMappingOverrride : IAutoMappingOverride<bounding_box>
{
public void Override(AutoMapping<bounding_box> mapping)
{
mapping.Map(x => x.area)
.CustomSqlType("geometry(Polygon)");
}
}
但是,我有许多带有几何列的表,因此我更希望能够指定自定义类型映射。
由于某些原因,以下属性约定永远不会拦截
area
属性:public class PostgisTypesConvention : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
if (instance.Type == typeof(Polygon))
{
instance.CustomSqlType("geometry(Polygon)"); // Never reached
}
}
}
如果我使用
GeoAPI.Geometries.IPolygon
而不是NetTopologySuite.Geometries.Polygon
,我也会遇到同样的问题... 最佳答案
我终于能够通过定义自定义UserTypeConvention
来解决此问题,即:
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)");
}
}
同样的原理也可以用于为其他几何(例如
UserTypeConventions
,Point
,LineString
等)创建MultiPoint
。