鉴于以下模型:
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)
列?请注意,我不关心是否能够使用NHibernate读取/更新几何列,因为我将在我的代码中使用GDAL。
我知道我可以通过实施手动覆盖来实现,即:
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
,我会遇到同样的问题...
答案 0 :(得分:0)
我终于能够通过定义自定义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
等。