Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

Dapper With Geography

Open somari984 opened this issue 3 years ago • 3 comments

I'm trying to retrieve SQL Geography using dapper through Stored procedure, but it's gives error: System.Data.DataException: 'Error parsing column 14 (GeoData=)' InvalidCastException: Unable to cast object of type 'Microsoft.SqlServer.Types.SqlGeography' to type 'Microsoft.Data.SqlClient.Server.IBinarySerialize'.

Dapper.EntityFramework.Handlers.Register(); added to ConfigureServices in startup

also tried to add `AssemblyLoadContext.Default.Resolving += Default_Resolving;

private static System.Reflection.Assembly Default_Resolving(AssemblyLoadContext assemblyLoadContext, System.Reflection.AssemblyName assemblyName) { try { AssemblyLoadContext.Default.Resolving -= Default_Resolving; return assemblyLoadContext.LoadFromAssemblyName(assemblyName); } catch { if (assemblyName.Name == "Microsoft.SqlServer.Types") return typeof(SqlGeography).Assembly; throw; } finally { AssemblyLoadContext.Default.Resolving += Default_Resolving; } }`

  • Dapper 2.0.123
  • Dapper.EntityFramework 2.0.90
  • .NET Core 6.0
  • Microsoft.EntityFrameworkCore.Relational 6.0.3
  • Microsoft.SqlServer.Types 14.0.1016.290
  • NetTopologySuite.IO.GeoJSON 2.0.4

somari984 avatar Apr 20 '22 06:04 somari984

That library targets .net framework; I'm actually impressed you got it to load at all on .net 6

The IBinarySerialize cast: that isn't from Dapper's code - so: I doubt this is a simple fix. I don't know, without doing a lot of digging, the correct way to approach this. Suggestions welcome.

On Wed, 20 Apr 2022, 07:12 somari984, @.***> wrote:

I'm trying to retrieve SQL Geography using dapper through Stored procedure, but it's gives error: System.Data.DataException: 'Error parsing column 14 (GeoData=)' InvalidCastException: Unable to cast object of type 'Microsoft.SqlServer.Types.SqlGeography' to type 'Microsoft.Data.SqlClient.Server.IBinarySerialize'.

Dapper.EntityFramework.Handlers.Register(); added to ConfigureServices in startup

also tried to add `AssemblyLoadContext.Default.Resolving += Default_Resolving;

private static System.Reflection.Assembly Default_Resolving(AssemblyLoadContext assemblyLoadContext, System.Reflection.AssemblyName assemblyName) { try { AssemblyLoadContext.Default.Resolving -= Default_Resolving; return assemblyLoadContext.LoadFromAssemblyName(assemblyName); } catch { if (assemblyName.Name == "Microsoft.SqlServer.Types") return typeof(SqlGeography).Assembly; throw; } finally { AssemblyLoadContext.Default.Resolving += Default_Resolving; } }`

  • Dapper 2.0.123
  • Dapper.EntityFramework 2.0.90
  • .NET Core 6.0
  • Microsoft.EntityFrameworkCore.Relational 6.0.3
  • Microsoft.SqlServer.Types 14.0.1016.290
  • NetTopologySuite.IO.GeoJSON 2.0.4

— Reply to this email directly, view it on GitHub https://github.com/DapperLib/Dapper/issues/1774, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAEHMBYM6ZOD2YWYC6ZBCDVF6N4BANCNFSM5T25T5YQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

mgravell avatar Apr 20 '22 07:04 mgravell

this exception when dapper try to read Geography as (datareader[fieldIndex]) when using microsoft.data.sqlclient , but when using system.data.sqlclient not giving such error. https://stackoverflow.com/questions/63210423/net-core-sql-server-geography-data-type-exception

here sample code to get same error

SpacialDataController.zip

somari984 avatar Apr 22 '22 06:04 somari984

Well then a part of me wants to say "use the one that works, then". Are you able to suggest/propose what the fix would be here? Happy to consider PRs.

On Fri, 22 Apr 2022, 07:24 somari984, @.***> wrote:

this exception when dapper try to read Geography as (datareader[fieldIndex]) when using microsoft.data.sqlclient , but when using system.data.sqlclient not giving such error.

https://stackoverflow.com/questions/63210423/net-core-sql-server-geography-data-type-exception

here sample code to get same error

SpacialDataController.zip https://github.com/DapperLib/Dapper/files/8537473/SpacialDataController.zip

— Reply to this email directly, view it on GitHub https://github.com/DapperLib/Dapper/issues/1774#issuecomment-1106054296, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAEHMBYVKFZGXWSUJUIQULVGJA3LANCNFSM5T25T5YQ . You are receiving this because you commented.Message ID: @.***>

mgravell avatar Apr 22 '22 17:04 mgravell