demetrixbio / FSharp.Data.Npgsql

F# type providers to support statically typed access to input parameters and result set of sql statement in idiomatic F# way. Data modifications via statically typed tables.
Apache License 2.0
128 stars 15 forks source link

Error reading Postgis - Geometry column #93

Closed sandeepc24 closed 4 years ago

sandeepc24 commented 4 years ago

I am getting following error when reading Geometry column from table.

System.InvalidOperationException: 'Expected column "geom" of type "System.Object" at position 6 (0-based indexing) but received column "geom" of type "NetTopologySuite.Geometries.Geometry".'

The column in database table is declared as

geom geometry(Point,4326),

Using Npgsql works correctly.

I have following set but it looks like the type provider is not respecting it.

NpgsqlConnection.GlobalTypeMapper.UseNetTopologySuite() |> ignore

following is the test app code

// Learn more about F# at http://fsharp.org

open System
open Npgsql

NpgsqlConnection.GlobalTypeMapper.UseNetTopologySuite() |> ignore

open FSharp.Data.Npgsql
open System.Runtime.InteropServices

module DbConnection =
    [<Literal>]
    let ConnectionStringName = "ConnectionStringName"

    let private IsWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
    let private ConnectionStringSeparator = if IsWindows then ":" else "__"
    let AppConnectionString = lazy Environment.GetEnvironmentVariable("ConnectionStrings" + ConnectionStringSeparator + ConnectionStringName)

type AppProvider = NpgsqlConnection<DbConnection.ConnectionStringName, ConfigType = ConfigType.Environment>

let openConnection() = 
    let con = new Npgsql.NpgsqlConnection(DbConnection.AppConnectionString.Value)
    con.Open()
    con.TypeMapper.UseNetTopologySuite() |> ignore
    con

let testNpgsql() =
    let conStr = @"mycon";
    use con = new Npgsql.NpgsqlConnection(conStr)
    con.Open()
    use cmd = new Npgsql.NpgsqlCommand("select geom from addresses", con)
    use reader = cmd.ExecuteReader()
    reader.Read() |> ignore
    printfn "%A" reader.[0]

let testTypeProvider() =
    use con = openConnection()
    use trn = con.BeginTransaction()
    use cmd = 
        AppProvider.CreateCommand<"select * from addresses", XCtor = true>(con, trn)
    let address = cmd.Execute() |> List.head
    printfn "%A" (address.geom)

[<EntryPoint>]
let main argv =
    //testNpgsql()
    testTypeProvider()
    0 // return an integer exit code
sandeepc24 commented 4 years ago

Anyone any clues as to why this is broken?

daz10000 commented 4 years ago

Fixed in 0.2.10