我有一个 SQL Server 2012 数据库,其中包含一个包含地理列的表,我想在使用该数据库的 .Net 应用程序中使用 Dapper,但据我所知并看到在 Dapper 代码中,“仅” Entity Framework 的 DBGeography 类型是 supported ,基础 SQLGeography 数据类型在存储库中没有其他提及。
Dapper 能否“神奇地”处理这些列类型,还是我必须为这些显式编写 Dapper.SqlMapper.TypeHandler?
最佳答案
在下一个版本中添加了对 SqlGeography
的支持,再次通过 。我还没有构建/部署,因为我有两种想法,这是否是最适合它居住的程序集......但我也不想依赖 Dapper.EntityFramework
包Microsoft.SqlServer核心库中的.Types
。不过,也许有一种方法可以不这样做。
更新:现在已经提升到核心库的级别,因此您不需要任何 EF 引用或 Dapper.EntityFramework
;它应该正常工作;这已被推送为 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; }
}
关于sql - 如何将 Dapper 与 MS SQL Server (2012) 地理空间/SQLGeography 列结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25538154/