Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 空间和Sql 2008地理类型-如何配置_C#_Nhibernate_Sql Server 2008_Fluent Nhibernate_Geography - Fatal编程技术网

C# 空间和Sql 2008地理类型-如何配置

C# 空间和Sql 2008地理类型-如何配置,c#,nhibernate,sql-server-2008,fluent-nhibernate,geography,C#,Nhibernate,Sql Server 2008,Fluent Nhibernate,Geography,我试图将Nhibernate与Sql 2008地理类型一起使用,但遇到了困难。我正在使用Fluent Nhibernate来配置我刚刚接触过的内容,所以这可能也是问题所在 首先,我试图坚持的类看起来像: public class LocationLog : FluentNHibernate.Data.Entity { public virtual new int Id {get;set;} public virtual DateTime TimeStamp {get;set;}

我试图将Nhibernate与Sql 2008地理类型一起使用,但遇到了困难。我正在使用Fluent Nhibernate来配置我刚刚接触过的内容,所以这可能也是问题所在

首先,我试图坚持的类看起来像:

public class LocationLog : FluentNHibernate.Data.Entity
{
   public virtual new int Id {get;set;}
   public virtual DateTime TimeStamp {get;set;}
   public virtual GisSharpBlog.NetTopologySuite.Geometries.Point Location {get;set;}
}
映射类如下所示:

public class LocationLogMap : ClassMap<LocationLog>
{
   ImportType<GisSharpBlog.NetTopologySuite.Geometries.Point>();
   Id(x => x.Id);
   Map(x => x.TimeStamp).Generated.Insert();
   Map(x => x.Location);
}
public class LocationLogMap:ClassMap
{
ImportType();
Id(x=>x.Id);
Map(x=>x.TimeStamp).Generated.Insert();
地图(x=>x.Location);
}
为了将MSSQLS2008GeographyDialect与Fluent Nhibernate一起使用,我创建了自己的配置类:

public class Sql2008Configuration
  : PersistenceConfiguration<Sql2008Configuration, MsSqlConnectionStringBuilder>
{
   public Sql2008Configuration()
   {
      Driver<SqlClientDriver>();
   }

   public static Sql2008Configuration MsSql2008
   {
      get { return new Sql2008Configuration().Dialect<MsSql2008GeographyDialect>(); }
   }
}
公共类Sql2008Configuration
:PersistenceConfiguration
{
公共Sql2008Configuration()
{
驱动器();
}
公共静态SQL2008MSSQL2008配置
{
获取{返回新的Sql2008Configuration().dialogue();}
}
}
因此,我有如下配置代码:

var configuration = Fluently.Configure()
  .Database(Sql2008Configuration.MsSql2008.ConnectionString(c => c.Is(connectionString)))
  .Mappings(m => m.FluentMappings
    .AddFromAssemblyOf<LocationLog>()
);
var-configuration=fluntly.Configure()
.Database(Sql2008Configuration.MsSql2008.ConnectionString(c=>c.Is(ConnectionString)))
.Mappings(m=>m.FluentMappings
.AddFromAssemblyOf()的
);
所有这些都是为了设置这样一个事实,即在尝试将LocationLog类型持久化到数据库时,我遇到以下错误:

期间发生.NET Framework错误 执行用户定义的例程或 综合“地理”: System.ArgumentException:24204:异常 空间参考标识符(SRID)为 无效。必须指定指定的SRID 匹配一个受支持的SRID 显示在 sys.spatical\u reference\u系统目录 看法System.ArgumentException:在 Microsoft.SqlServer.Types.SqlGeography.set_Srid(Int32 价值)在 Microsoft.SqlServer.Types.SqlGeography.Read(BinaryReader r) 在 SqlGeography::.DeserializeValidate(IntPtr ,Int32,CClrLobContext*)

我已经阅读了以下关于如何配置和使用Nhibernate空间库的文章:


但两者似乎都没有帮助。任何有配置Nhibernate使用空间地理类型经验的人,如果能够提供任何见解,都将不胜感激。

这不是真正的答案,而是问题;-)

  • 是否在GisSharpBlog.NetTopologySuite.Geometries.Point对象上设置SRID
默认值(因为该点是一个几何体)为0,并且在尝试将LocationLog.Location属性持久化为地理位置时会出现SQL错误。0不是sql地理字段的有效SRID。您需要从sys.spatial\u reference\u systems视图中指定一个

  • 你有没有试过不用流利的NHibernate

要从问题中消除尽可能多的组件。

Steve是对的,您需要为几何体类型显式设置一个。查看NHibernate.Spatial源代码(您可以使用SVN或其他任何工具进行签出),搜索SRID时,代码中会出现以下注释提示:

<class name="MyGeoTableA">
    <property name="MyGeoColumn">
        <type name="NHibernate.Spatial.Type.GeometryType, NHibernate.Spatial">
            <param name="srid">1234</param>
        </type>
    </property>
</class>

一定有更好的方法可以做到这一点(配置它而不是一直设置它),但我还没有解决这个问题。如果您查看MSSQLS2008GeometryType类内部,它有一个名为SetDefaultSRID(IGometry)的受保护方法-它一定是有原因的

我也在同一条船上,多亏了你的开始,我才让它工作起来(插入和读取空间数据)。对于其他感兴趣的人,首先,gisharpblog.NetTopologySuite.Geometries.Point类位于NetTopologySuite.dll中,它是nHibernate.spatical下载的一部分

其次,根据James point,确保将SRID设置为4326

最后,地图需要如下所示:

Map(a => a.Location).CustomType(typeof(NHibernate.Spatial.Type.GeometryType));
var point = Default.Factory.CreatePoint(new Coordinate(10, 10));

我使用的是地理,但我在某个地方读到,使用GeometryType可能有效,而且对我来说确实有效(我在数据库中插入了一些点并进行了验证)。我还读到,最好为地理编写SQL查询,这样您就可以使用特殊的SQL 2008空间方法(而不是使用条件)

您可以使用默认SRID创建自己的工厂。 例如,您可以为工厂创建立面,如下所示:

public static class Default
{
    private const int DefaultSrid = 4326;

    public static readonly IGeometryFactory Factory;

    static Default()
    {
        Factory = new GeometryFactory(new PrecisionModel(), DefaultSrid);
    }
}
然后像这样使用它:

Map(a => a.Location).CustomType(typeof(NHibernate.Spatial.Type.GeometryType));
var point = Default.Factory.CreatePoint(new Coordinate(10, 10));
而不是使用“new”关键字。
您还可以在IoC框架中使用Default.Factory作为工厂方法来创建没有默认外观的新几何体。

我知道这几乎没有什么用处,但无论如何都是如此。 在实现了lain所说的在HQL查询中使用SetParameter和第三个IType参数之后。意指

 Hero hero = openSession.Get<Hero>(3);    
openSession.CreateQuery(
              "from Hero h where NHSP.Distance(h.Location,:thislocation)<1000"
               ).SetParameter("thislocation", hero.Location, new CustomType(typeof(MsSql2008GeographyType), null) ).SetResultTransformer(new DistinctRootEntityResultTransformer())
               .List();
Hero-Hero=openSession.Get(3);
openSession.CreateQuery(
“距离h,其中NHSP.距离(h.位置,:此位置)