如何解决NHibernate:TINYINT代替几何图形
| 我正在使用NHibernate进行C#项目,我将fluant-nhibernate与autoMapping结合使用: FluentConfiguration configuration = Fluently.Configure()
.Database(databaseConfig)
.Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Entity>(mappingConfiguration).Conventions.Add<GeometryTypeConvention>()));
我有一个具有IGeometry属性的类,我已经用self Convention类型配置了自动映射:
public class GeometryTypeConvention : IUserTypeConvention
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(p => p.Property.PropertyType == typeof (IGeometry));
}
public void Apply(IPropertyInstance instance)
{
instance.CustomType(typeof(MsSql2008GeometryType));
}
}
更新架构时,将创建数据库,但将类中的所有Geometry属性设置为TINYINT列。
我在http://www.klopfenstein.net/lorenz.aspx/null-geometry-values-in-nhibernate-spatial-for-mssql2008中看到了几乎相同的问题,但是我使用的文件MsSql2008GeometryType.cs是正确的。
解决方法
我遇到了同样的问题,并以这种方式解决了这一问题(使用地理位置而不是几何,但是非常相似):
首先(此步骤是可选的),由于需要使用WGS84坐标,因此创建了以下类型:
public class Wgs84GeographyType : MsSql2008GeographyType
{
protected override void SetDefaultSRID(GeoAPI.Geometries.IGeometry geometry)
{
geometry.SRID = 4326;
}
}
然后,我创建了一个与您的约定类似的约定,但指定了\“ CustomSqlType \”方法:
public class Wgs84GeographyTypeConvention : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
if (typeof(IGeometry).IsAssignableFrom(instance.Property.PropertyType))
{
instance.CustomType(typeof(Wgs84GeographyType));
instance.CustomSqlType(\"GEOGRAPHY\");
}
}
}
之后,模式生成应该可以正常工作。
,您应该使用ѭ4来正确生成与空间相关的模式。使用Fluent NHibernate,它看起来像:
.ExposeConfiguration(
cfg =>
{
cfg.AddAuxiliaryDatabaseObject(new SpatialAuxiliaryDatabaseObject(cfg));
new SchemaExport(cfg).Create(true,true);
})
另外,在数据库配置中设置方言:
.Database(MsSqlConfiguration
.MsSql2008
.Dialect(typeof (MsSql2008GeometryDialect).AssemblyQualifiedName)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。