如何将Dapper与MS SQL Server(2012)地理空间/ SQLGeography列一起使用 [英] How to use Dapper with MS SQL Server (2012) Geospatial / SQLGeography Column

查看:100
本文介绍了如何将Dapper与MS SQL Server(2012)地理空间/ SQLGeography列一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL Server 2012数据库,该数据库的表包含一个地理列,我想在使用该数据库的.Net应用程序中使用Dapper,但据我所知在Dapper代码中,仅实体框架的DBGeography类型为受支持的,基础SQLGeography数据类型在存储库中没有其他提及。

I have a SQL Server 2012 database with a table that contains a geography column and I want to use Dapper in a .Net application working with that database, but as far as I can tell and see in the Dapper code, "only" Entity Framework's DBGeography type is supported, the underlying SQLGeography data type has no other mentioning in the repository.

Dapper能否神奇地处理这些列类型还是我必须为这些显式编写一个Dapper.SqlMapper.TypeHandler?

Can Dapper handle these column types 'magically' nevertheless or would I have to explicitly write a Dapper.SqlMapper.TypeHandler for these?

推荐答案

SqlGeography的支持再次通过 Dapper.EntityFramework 包添加到了下一个版本中。我还没有构建/部署,因为我是否认为这是最适合居住的程序集……我有两种想法,但我也不想依赖核心库中的Microsoft.SqlServer.Types

更新:现在,它已经升级到了核心库,因此您不需要任何EF引用或 Dapper.EntityFramework ;它应该正常工作;已被推送为 Dapper 1.32

Update: this has now moved up a level to the core library, so you shouldn't need any EF references or Dapper.EntityFramework; it should just work; this has been pushed as Dapper 1.32.

示例:

public void SqlGeography_SO25538154()
{
    Dapper.SqlMapper.ResetTypeHandlers(); // to show it doesn't depend on any
    connection.Execute("create table #SqlGeo (id int, geo geography)");

    var obj = new HazSqlGeo
    {
        Id = 1,
        Geo = SqlGeography.STLineFromText(
            new SqlChars(new SqlString(
                "LINESTRING(-122.360 47.656, -122.343 47.656 )")), 4326)
    };
    connection.Execute("insert #SqlGeo(id, geo) values (@Id, @Geo)", obj);
    var row = connection.Query<HazSqlGeo>(
        "select * from #SqlGeo where id=1").SingleOrDefault();
    row.IsNotNull();
    row.Id.IsEqualTo(1);
    row.Geo.IsNotNull();
}

class HazSqlGeo
{
    public int Id { get; set; }
    public SqlGeography Geo { get; set; }
}

这篇关于如何将Dapper与MS SQL Server(2012)地理空间/ SQLGeography列一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆