How to use Dapper with MS SQL Server (2012) Geospatial Data Column / SQLGeography

I have a SQL Server 2012 database with a table containing a geography column, and I want to use Dapper in a .Net application working with this database, but as far as I can tell and see in the Dapper code β€œonly” the DBGeography Entity Framework type is supported , The underlying SQLGeography data type is not mentioned in the repository.

Can Dapper handle these column types magically, though, or do I need to explicitly write Dapper.SqlMapper.TypeHandler for these?

+5
sql sql-server dapper
source share
1 answer

SqlGeography support was added in the next version, again through the Dapper.EntityFramework package . I have not built / deployed yet, as I have two minds about whether this is the most suitable assembly for this to live ... but I also do not want to depend on Microsoft.SqlServer.Types in the kernel library. However, there may be a way to do this without it.


Update: now it has moved to the level in the main library, so you do not need any EF or Dapper.EntityFramework ; it should just work; it was clicked like Dapper 1.32 .

Example:

 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; } } 
+6
source share

All Articles