开发者

NHibernate : TINYINT instead of geometry

I'm working on a C# project using NHibernate, I use fluant-nhibernate with autoMapping :

        FluentConfiguration configuration = Fluently.Configure()
            .Database(databaseConfig)
            .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Entity>(mappingConfiguration).Conventions.Add<GeometryTypeConvention>()));

I have a classes with IGeometry properties, I have configured automapping with a self Convention type :

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));
    }
}

When I update the schema, the database is created but all Geometry properties in classes are set as TINYINT columns.

I've seen almost the same problem on http://www.klopfenstein.net/lorenz.as开发者_如何学编程px/null-geometry-values-in-nhibernate-spatial-for-mssql2008, but the file MsSql2008GeometryType.cs I use is correct.


I had the same problem, and I solved it this way (using geography instead of geometry, but it's very similar):

First (this step is optional), and because I was required to work with WGS84 coordinates, I created the following type:

public class Wgs84GeographyType : MsSql2008GeographyType
{
    protected override void SetDefaultSRID(GeoAPI.Geometries.IGeometry geometry)
    {
        geometry.SRID = 4326;
    }
}

Then I created a convention somehow similar to yours, but with the "CustomSqlType" method specified:

public class Wgs84GeographyTypeConvention : IPropertyConvention
{
    public void Apply(IPropertyInstance instance)
    {
        if (typeof(IGeometry).IsAssignableFrom(instance.Property.PropertyType))
        {
            instance.CustomType(typeof(Wgs84GeographyType));
            instance.CustomSqlType("GEOGRAPHY");
        }
    }
}

Afterwards the schema generation should work without any issue.


You should use SpatialAuxiliaryDatabaseObject in order to properly generate spatial related schema. Using Fluent NHibernate, this would look like:

    .ExposeConfiguration(
        cfg =>
        {
            cfg.AddAuxiliaryDatabaseObject(new SpatialAuxiliaryDatabaseObject(cfg));
            new SchemaExport(cfg).Create(true, true);
        })

Also, set the dialect in the database configuration:

    .Database(MsSqlConfiguration
        .MsSql2008
        .Dialect(typeof (MsSql2008GeometryDialect).AssemblyQualifiedName)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜