Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 795775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:31:58+00:00 2026-05-14T22:31:58+00:00

I’ve been having quite a few problems trying to use Microsoft.SqlServer.Types.SqlGeography . I know

  • 0

I’ve been having quite a few problems trying to use Microsoft.SqlServer.Types.SqlGeography. I know full well that support for this in Linq to Sql is not great. I’ve tried numerous ways, beginning with what would the expected way (Database type of geography, CLR type of SqlGeography). This produces the NotSupportedException, which is widely discussed via blogs.

I’ve then gone down the path of treating the geography column as a varbinary(max), as geography is a UDT stored as binary. This seems to work fine (with some binary reading and writing extension methods).

However, I’m now running into a rather obscure issue, which does not seem to have happened to many other people.

System.InvalidCastException: Unable to cast object of type ‘Microsoft.SqlServer.Types.SqlGeography’ to type ‘System.Byte[]’.

This error is thrown from an ObjectMaterializer when iterating through a query. It seems to occur only when the tables containing geography columns are included in a query implicitly (ie. using the EntityRef<> properties to do joins).

System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()

My question: If I’m retrieving the geography column as varbinary(max), I might expect the reverse error: can’t cast byte[] to SqlGeography. That I would understand. This I don’t. I do have some properies on the partial LINQ to SQL classes that hide the binary conversion… could those be the issue?

Any help appreciated, and I know there’s probably not enough information.

Extras:

  • A geography column in the Visual Studio dbml Designer with ‘Server Data Type’ = geography generates this error: The specified type 'geography' is not a valid provider type.
  • A geography column in the Visual Studio dbml Designer with no ‘Server Data Type’ generates this error: Could not format node 'Value' for execution as SQL.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:31:58+00:00Added an answer on May 14, 2026 at 10:31 pm

    Spatial types are not supported by Linq to SQL. Support is not “not great” – it’s nonexistent.

    You can read them as BLOBs, but you can’t do that by simply changing the column type in Linq to SQL. You need to alter your queries at the database level to return the column as a varbinary, using the CAST statement. You can do this at the table level by adding a computed varbinary column, which Linq will happily map to a byte[].

    In other words, some DDL like this:

    ALTER TABLE FooTable
    ADD LocationData AS CAST(Location AS varbinary(max))
    

    Then, remove the Location column from your Linq to SQL class, and use LocationData instead.

    If you then need access to the actual SqlGeography instance, you’ll need to convert it to and from the byte array, using STGeomFromWKB and STAsBinary.

    You can make this process a bit more “automatic” by extending the partial Linq to SQL entity class and adding an auto-converting property:

    public partial class Foo
    {
        public SqlGeography Location
        {
            get { return SqlGeography.STGeomFromWKB(LocationData, 4326); }
            set { LocationData = value.STAsBinary(); }
        }
    }
    

    This assumes that LocationData is the name of the computed varbinary column; you don’t include the “real” Location column in your Linq to SQL definition, you add it in the ad-hoc fashion above.

    Note also that you won’t be able to do much with this column other than read and write to it; if you try to actually query on it (i.e. including it in a Where predicate) then you’ll just get a similar NotSupportedException.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 408k
  • Answers 408k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use the return statement to return a value… May 15, 2026 at 6:46 am
  • Editorial Team
    Editorial Team added an answer This works in Firefox. It should also work in IE6.… May 15, 2026 at 6:46 am
  • Editorial Team
    Editorial Team added an answer When you quote an array variable like this: "@array" it… May 15, 2026 at 6:46 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.