Search code examples
postgresqlf#.net-coretype-providersf#-4.0

F# Connect SQLProvider with Postgres


I'm a beginner in both F# and the .Net world, I'm trying to make this F# script work:

#r "./packages/SQLProvider/lib/netstandard2.0/FSharp.Data.SqlProvider.dll"
#r "./packages/SQLProvider/lib/netstandard2.0/netstandard.dll"
#r "./packages/Npgsql/lib/netstandard2.0/Npgsql.dll"

open FSharp.Data.Sql
open Npgsql

let [<Literal>] ConnString = 
    "Host=localhost;"       +
    "Port=5431;"            +
    "Database=suavetododb;" +
    "Username=postgres;"    +
    "Password=postgres"

let [<Literal>] DbVendor = Common.DatabaseProviderTypes.POSTGRESQL
let [<Literal>] ResPath = @"./packages/Npgsql/lib/netstandard2.0"

type Sql =
    SqlDataProvider<
        DbVendor,
        ConnString,
        "",
        ResPath,
        1000,
        true>

This is the error I get:

error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: Could not resolve field token 0x04000523, due to: Could not load type of field 'Npgsql.NpgsqlConnection+d__28:<>u__2' (7) due to: Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. assembly:System.Threading.Tasks.Extensions, Version=4.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 type: member:(null) signature: assembly:(my_filesystem)/API/packages/Npgsql/lib/netstandard2.0/Npgsql.dll type:d__28 member:(null) signature:

Which is weird, since Npgsql is actually imported (so the last #r actually works).

The database is up via docker:

$ docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                    NAMES
4704763ec2ba        suave_rezoom_db:0.1   "docker-entrypoint.s…"   25 hours ago        Up 2 hours          0.0.0.0:5431->5432/tcp   todo_rezoom_1

Also, the project was generated via the .Net Core 2.1.200 and is running on Linux.


Solution

  • This is because the type provider needs to resolve dependencies of the third-party driver (Npgsql in this case), and it can't because it doesn't have access to the full dependency tree like NuGet (or Paket) does. You need to copy System.Threading.Tasks.Extensions.dll to the same folder where Npgsql.dll sits (your ResPath) so that the type provider can resolve that assembly.

    Once you do that, you'll find that you also need to copy System.Runtime.CompilerServices.Unsafe.