3

In f# I'm attempting create a SqlGeography type and save it to a record. When SubmitChanges is called at runtime, it returns the following error message.

Also, I am using LinqPad if that matters.

InvalidCastException: Could not convert from type 'Microsoft.SqlServer.Types.SqlGeography' to type 'Microsoft.SqlServer.Types.SqlGeography'.

let dc = new TypedDataContext()

let lat = 45.523062
let long = -122.676482
let srid = 4326

let property = query {
                    for prop in dc.Property do
                    where (prop.PropertyID = 15957)
                    select prop
                    exactlyOne
                }

let sqlGeogBuild = new SqlGeographyBuilder()
sqlGeogBuild.SetSrid(srid)
sqlGeogBuild.BeginGeography(OpenGisGeographyType.Point)
sqlGeogBuild.BeginFigure(lat, long)
sqlGeogBuild.EndFigure()
sqlGeogBuild.EndGeography()

property.Location <- sqlGeogBuild.ConstructedGeography.MakeValid()

//property.Dump()

dc.SubmitChanges()
mac10688
  • 2,145
  • 2
  • 24
  • 37
  • I thought I got this working with c#, but I can't. Maybe it's because Linqpad uses linq to sql and sqlgeography isn't supported with that. I'm not sure. If that was the case what are my options that allow me to use linqpad? – mac10688 Apr 14 '16 at 05:34
  • 3
    Does this answer regarding a type version mismatch help? http://stackoverflow.com/questions/25323301/sqlgeography-type-mismatch – Liesel Apr 14 '16 at 08:14
  • That looks likely! I just need to figure out how to confirm this. Using ildasm I can see that I am using version 11 of SqlServer.Types in linqpad. I don't know how to see what version sql is using though. – mac10688 Apr 14 '16 at 15:16
  • I downloaded version 10 from nuget packages and used that. That error went away but now I am seeing "Could not format node 'Value' for execution as SQL" – mac10688 Apr 14 '16 at 15:34
  • Try searching for that error and several answers that might be of use come up. – Liesel Apr 15 '16 at 01:33
  • No, their issue is that they are creating new types in their predicate clause that SQL doesn't know about. I mean it is probably what's going on here. Linq to SQL can't handle sqlgeography, but I was hoping it would somehow. – mac10688 Apr 15 '16 at 01:37
  • I don't mean to be flippant, but search really is your friend. "LINQ to SQL SQLGeography" into google gives tons of hits including http://stackoverflow.com/questions/1892381/linq-unsupported-data-types-geography http://stackoverflow.com/questions/2845767/is-it-possible-to-use-sqlgeography-with-linq-to-sql which may (or may not) be of use. – Liesel Apr 15 '16 at 02:09
  • I did search for hours. I've seen all those posts. I was hoping for something better because those posts are 6-7 years old. Thank you. – mac10688 Apr 15 '16 at 02:13

0 Answers0