Home

Invalid Cast Option for SqlGeography

on running a query I am getting the following error.

InvalidCastException: [A]Microsoft.SqlServer.Types.SqlGeography cannot be cast to [B]Microsoft.SqlServer.Types.SqlGeography. Type A originates from 'Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' in the context 'Default' at location 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.SqlServer.Types\10.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll'. Type B originates from 'Microsoft.SqlServer.Types, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' in the context 'Default' at location 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.SqlServer.Types\13.0.0.0__89845dcd8080cc91\Microsoft.SqlServer.Types.dll'.

The query in question was working fine less than 12 hours ago and nothing has changed apart from me logging off and back on this morning.

Comments

  • It could be that another assembly has been added to the GAC.

    Does your query use Entity Framework, and does it reference any extra assemblies?
  • I did a C# query from a table in our SQL Server Database that has a geography column, query "LocationHistories.Take(5)" using linq to sql and it does not have any other references and I got the same error. I was able to access the table before the v5.25 update and Linqpad 4 is able to access these tables.
  • Can you please try right-clicking the connection, choosing 'Refresh', and tell me if it makes any difference.
  • Also, can you double-check that there are no references in "My Extensions"?
  • I've right clicked the connection and chose refresh, this made no difference. I checked My Extensions and there's just reference Microsoft.Office.Interop.Excel and removed that and retried everything and it still does not work.
    Sql Server we are using is version 2016 SP1, build version 13.0.4001.0
  • edited November 2017
    I've examined the code carefully and found a potential bug in the way Microsoft.SqlServer.Types.dll is resolved. Try the following new beta and let me know if it makes any difference:

    https://www.linqpad.net/download.aspx#beta

    (Note that after running the beta, you will need to right-click the connection and refresh again.)

    If it still throws, can you provide more information on the exception? Can you expand it, and if so, does it show a stack trace? Also, can you run the following query and tell me the output:

    http://share.linqpad.net/fgag4q.linq
  • edited November 2017
    With the beta I can at least query the table now. I'm still missing the location column when using c#, but I can get the column when I run sql which is fine.

    The output of the query you ask me to run is:
    Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91
    Microsoft.SqlServer.Types, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91
    Microsoft.SqlServer.Types, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91
    Microsoft.SqlServer.Types, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91
    Microsoft.SqlServer.Types, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91
Sign In or Register to comment.