Mapping Geometry from SQLServer2008 to .NET (NHibe

2019-07-20 12:53发布

I try to map a SqlServer2008 geometry with FluentNHibernate. I am using NHibernate version 4.0.0.4000. I installed NHibernate.Spatial, NetTopologySuite, GeoAPI and NHibernate with FluentNhibernate all with NUget.

My Fluent mapping looks like this:

public class ArealMap: ClassMap<Areal>
{
    public Areal()
    {
        Table("Areal");
        Id(x => x.Id).Column("Id").GeneratedBy.Identity();
        Map(x => x.Geometry).Column("Geometry").CustomType(typeof(MsSql2008GeometryType));
    }
}
public class Areal 
{
   ....
   public virtual Geometry Geometry{ get; set; }
}

With NHibernate 4.0.0.4000 Areal will always be mapped to null instead of the value in the DB (of type geometry). The non-geometry properties are mapped correctly.

This mapping was working perfectly in NHibernate 3.3.3. I also added

 .Dialect<MsSql2008GeometryDialect>())

to my fluent configuration....

I think all the dlls have the correct version installed, as I got them via NUget. I don´t think its an NH /Fluent NH issue as all my other mappings are working fine...

Is there a new syntax for mapping sql server geometry to NHibernate in the release for NH 4.0? I don´t know what I am missing..

EDIT:

When I change the mapping to:

Map(x => x.Geometry).Column("Geometry").CustomType(typeof(MsSqlLegacyGeometryType));

I don´t get null in Geometry anymore, instead my polygon in database is written to a mapped as a Geometry of type point with different coordinates....

I think this problem might be caused by different versions of Microsoft.SqlServer.Types beeing used... SqlServer2008 uses version 10.0 and NHibernate.Spatial.MsSql uses version 11.0... Or there may be breaking changes in GeoApi or NetTopologySuite...

EDIT:

Okay, I found the source of the problem....

I am using Sql Server 2008, so it uses Microsoft.SqlServer.Types.dll in

C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.Types\10.0.0.0__89845dcd8080cc91\

However, the NHibernate.Spatial dlls which work with NHibernate 4.0 expect higher SQLServer and reference Microsoft.SqlServer.Types.dll version 11 in

C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.Types\11.0.0.0_89845dcd8080cc91\

Anyone know how to fix this issue?

I think I have to checkout / clone one of the NHibernate.Spatial projects and manually reference Microsoft.SqlServer.Types.dll version 10 and then recompile the project...

I am lost however as where to start. What projects do I need to reoompile? NetTopologySuite which is referenced by NHibernate.Spatial or only NHibernate.Spatial?

1条回答
戒情不戒烟
2楼-- · 2019-07-20 13:29

While perhaps not ideal since it's forcing you to use an older version, I was able to solve a mismatch involving the Geometry DLLS after an NHibernate upgrade by using a binding redirect on the assembly declaration in the web.config

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
     <dependentAssembly>
      <assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="10.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>
查看更多
登录 后发表回答