0

I'm using ASP.NET Core 7, and I am trying to read in spatial data from a SQL Server. I'm trying to execute a query and map it to a strongly typed list:


public class MyDataSchema {
    public int id {get; set;}
    public SqlGeography spatialData {get; set;}
}

using (SqlConnection con = new SqlConnection('ConnectionStringHere'){
   var data = con.Query<MyDataSchema>("Select * from <Table_Name>").ToList();
}

This code works for all data except for when the spatialData column is included. In that case it throws the following error:

An exception of type 'System.Data.DataException' occurred in Dapper.dll but was not handled in user code: 'Error parsing column 2 (spatialData=1234 - Double)' Microsoft.SqlServer.Server.InvalidUdtException : 'Microsoft.SqlServer.Types.SqlGeography' is an invalid user defined type, reason: no UDT attribute.

I believe the SQL Server update to 2012 broke it link. I cannot change the version of the SQL Server, and I wasn't sure how to implement the example using Dapper. Has anyone had experience with this?

I tried implementing a Dapper SQLMapper Type handler for the SQLGeography, but the error still occurred despite adding the type handler via SqlMapper.AddTypeHandler(new SpatialDataHandler());.

  • here's a similar case,have you tried with it?:https://stackoverflow.com/questions/41459316/cannot-read-sqlgeography-with-dapper – Ruikai Feng Apr 25 '23 at 06:41
  • Do you use System.Data.SqlClient or Microsoft.Data.SqlClient? For me it helped to switch to using the Microsoft.Data.SqlClient package. – Abra Apr 26 '23 at 15:04
  • Does this answer your question? [Cannot read SqlGeography with Dapper](https://stackoverflow.com/questions/41459316/cannot-read-sqlgeography-with-dapper) – Amit Joshi May 16 '23 at 07:26

0 Answers0