Is it possible to use SqlGeography with Linq to Sql?
Posted
by cofiem
on Stack Overflow
See other posts from Stack Overflow
or by cofiem
Published on 2010-05-16T22:41:41Z
Indexed on
2010/05/16
22:50 UTC
Read the original article
Hit count: 300
I've been having quite a few problems trying to use Microsoft.SqlServer.Types.SqlGeography
. I know full well that support for this in Ling 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 only occur 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.
© Stack Overflow or respective owner