Guton
Guton

Reputation: 37

NH spatial and Fluent Mapping

I have read this in order to compile NH spatial for Nhibernate 3.1

http://build-failed.blogspot.it/2012/02/nhibernate-spatial-part-2.html

I have also read this

NHibernate.Spatial and Sql 2008 Geography type - How to configure

but the same code for me don't compile... I have this

using NetTopologySuite.Geometries;
    namespace Core
    {
        public class District
        {
            public virtual int Id { get; set; }
            public virtual Polygon Area { get; set; }
            public virtual string Name { get; set; }
        }
    }

    using Core;
    using FluentNHibernate.Mapping;

    namespace TestNHSpatial
    {
        public class DistrictMap : ClassMap<District>
        {
            public DistrictMap()
            {
                ImportType<NetTopologySuite.Geometries.Point>();

                Id(x => x.Id);
                Map(x => x.Name);
                Map(x => x.Area).CustomType<Wgs84GeographyType>();
            }
        }
    }

and this

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

finally

var cfg = new OrderingSystemConfiguration();

var configuration = Fluently.Configure()
    .Database(MsSqlConfiguration.MsSql2008
    .ConnectionString(connString)
    .Dialect<MsSql2008GeographyDialect>())
    .Mappings(m => m.AutoMappings.Add(
        AutoMap.AssemblyOf<District>(cfg)))
    .BuildConfiguration();

var exporter = new SchemaExport(configuration);

exporter.Drop(false, true);

exporter.Create(true, true);

i have this error...

NHibernate.MappingException : An association from the table District refers to an unmapped class: NetTopologySuite.Geometries.Polygon

can anyone help me?

thanks

Upvotes: 0

Views: 1023

Answers (1)

psousa
psousa

Reputation: 6726

UPDATE:

The code has some issues, namely:

  • You're using AutoMappings. You need to use custom mappings
  • You're using the wrong assembly when searching for the mappings
  • The export schema code is incorrect.

I'm the author of the blog post that you refer.

Change the type from Polygon (from NetTopologySuite) to IPolygon(from GeoAPI).

Should be something like this:

using GeoAPI.Geometries;

public class District
{
    public virtual int Id { get; set; }

    public virtual IPolygon Area { get; set; }        

    public virtual string Name { get; set; }

}

Anyway, if this doesn't work, send me a zip with a test project and I'll check it out.

Upvotes: 1

Related Questions