Question

Is it possible to access an SQL Server 2008 table with a column of type Geography using a Type Provider in F#?

This is my connection:

type dbSchemaAnalyticsWeb = SqlDataConnection<"Data Source=sql2008;Initial Catalog=Analytics;User ID=USER;Password=PASSWORD;">

Then the code to insert data:

let dbAnalyticsSQL = dbSchemaAnalyticsWeb.GetDataContext()

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

            let LData = new dbSchemaAnalyticsWeb.ServiceTypes.LocationsData(
                                       Address = address,
                                       ZipCode = zipCode,                                          
                                       Longitude = long,
                                       Latitude = lat,
                                       GeoLocation = sqlGeogBuild.ConstructedGeography) 

In the code above, the field GeoLocation references the Geography data type from SQL Server. However I get an error "The member or object constructor 'LocationData' has no argument or settable return property 'GeoLocation'. Is there another way to reference this field, or some other interface, or is this data type simply not accessible?

Note, I am using Visual Studio 2012 and .Net 4.5.

Was it helpful?

Solution

You are using the LinqToSql provider, which does not have support for the SqlGeography type. Try using the Entity Framework provider.

Here is how I reference a database that uses SqlGeography in one of my projects:

type internal Database = SqlEntityConnection<ConnectionStringName="DefaultConnection", LocalSchemaFile="Context.ssdl", ConfigFile="Web.config", ForceUpdate=true, Pluralize=true>

For most purposes the LinqToSql, and Entity Framework providers are very similar. The main difference is that the Entity Framework provider exposes types as internal, while LinqToSql makes things public.

If for some reason you would rather stay with LinqToSql, there are some work-arounds that might allow you to do what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top