gpt4 book ai didi

c# - NHibernate.Spatial 和 Sql 2008 地理类型 - 如何配置

转载 作者:IT王子 更新时间:2023-10-29 04:31:24 27 4
gpt4 key购买 nike

我正在尝试将 Nhibernate 与 Sql 2008 地理类型一起使用,但遇到了困难。我正在使用 Fluent Nhibernate 来配置我对它相当陌生,所以这也可能是问题所在。

首先,我要坚持的类(class)看起来像这样:

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

为了在 Fluent Nhibernate 中使用 MsSql2008GeographyDialect,我创建了自己的配置类:

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

public static Sql2008Configuration MsSql2008
{
get { return new Sql2008Configuration().Dialect<MsSql2008GeographyDialect>(); }
}
}

所以我有这样的配置代码:

var configuration = Fluently.Configure()
.Database(Sql2008Configuration.MsSql2008.ConnectionString(c => c.Is(connectionString)))
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf<LocationLog>()
);

所有这些都是为了设置我在尝试将 LocationLog 类型持久保存到数据库时收到以下错误的事实:

A .NET Framework error occurred during execution of user-defined routine or aggregate "geography": System.ArgumentException: 24204: The spatial reference identifier (SRID) is not valid. The specified SRID must match one of the supported SRIDs displayed in the sys.spatial_reference_systems catalog view. System.ArgumentException: at Microsoft.SqlServer.Types.SqlGeography.set_Srid(Int32 value) at Microsoft.SqlServer.Types.SqlGeography.Read(BinaryReader r) at SqlGeography::.DeserializeValidate(IntPtr , Int32 , CClrLobContext* )

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

但似乎都没有帮助。任何有配置 Nhibernate 以使用空间地理类型的经验的人都可以提供任何见解,我们将不胜感激。

最佳答案

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

其次,按照 James 的观点,确保将 SRID 设置为 4326。

最后, map 需要如下所示:

Map(a => a.Location).CustomType(typeof(NHibernate.Spatial.Type.GeometryType));

我正在使用 Geography,但我在某处读到使用 GeometryType 可能有用并且对我有用(我插入了一些点并在数据库中验证了它)。我还读到最好为地理编写 SQL 查询,以便您可以使用特殊的 SQL 2008 Spatial 方法(而不是使用 Criteria)。

关于c# - NHibernate.Spatial 和 Sql 2008 地理类型 - 如何配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1495169/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com