fsprojects / SQLProvider

A general F# SQL database erasing type provider, supporting LINQ queries, schema exploration, individuals, CRUD operations and much more besides.
https://fsprojects.github.io/SQLProvider
Other
572 stars 146 forks source link

Could not load type of field 'Npgsql.NpgsqlConnection+... #609

Closed MiYogurt closed 5 years ago

MiYogurt commented 5 years ago

Description

SQLProvider not support macos ? i see the post http://fsprojects.github.io/SQLProvider/core/netstandard.html#NET-Standard-NET-Core-support

it look like supported.

but i got error

➜  flower_crawler git:(master) dotnet build
Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 81.95 ms for /Users/xx/Projects/flower_crawler/flower_crawler/flower_crawler.fsproj.
/Users/xxx/Projects/flower_crawler/flower_crawler/DB.fs(11,12): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: Could not resolve field token 0x04000a32, due to: Could not load type of field 'Npgsql.NpgsqlConnection+<>c__DisplayClass32_0+<<Open>g__OpenLong|0>d:<>u__2' (5) due to: Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. assembly:/Users/xxx/.nuget/packages/npgsql/4.0.6/lib/netstandard2.0/Npgsql.dll type:<<Open>g__OpenLong|0>d member:(null) 
......
[/Users/xxx/Projects/flower_crawler/flower_crawler/flower_crawler.fsproj]

Build FAILED.

/Users/xxx/Projects/flower_crawler/flower_crawler/DB.fs(11,12): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: Could not resolve field token 0x04000a32, due to: Could not load type of field 'Npgsql.NpgsqlConnection+<>c__DisplayClass32_0+<<Open>g__OpenLong|0>d:<>u__2' (5) due to: Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. assembly:/Users/xxx/.nuget/packages/npgsql/4.0.6/lib/netstandard2.0/Npgsql.dll type:<<Open>g__OpenLong|0>d member:(null) 
...
[/Users/xxx/Projects/flower_crawler/flower_crawler/flower_crawler.fsproj]
    0 Warning(s)
    15 Error(s)

Time Elapsed 00:00:11.35

i tested . the Npgsql no problem

open System
open Npgsql
open FSharp.Data.Sql

type sql = SqlDataProvider<Common.DatabaseProviderTypes.POSTGRESQL, "Host=localhost;Username=yugo;Password=yugo;Database=test_db"> // error in here, as same as top

[<EntryPoint>]
let main argv =
    let testConn = "Host=localhost;Username=yugo;Password=yugo;Database=test_db";
    let conn = new NpgsqlConnection(testConn)
    try
        printfn "Trying to open a connection"
        //let ctx = sql.GetDataContext() // error in here  no method
        //Console.WriteLine(ctx) // error in here
        conn.Open() // it work
    with ex ->
        printfn "Exception trying to open conn:\n%O" ex
    0

It's too difficult for beginners. Especially for non-Windows user ,i am hope has some docker's sample

Repro steps

all code in here,i just use f# crawl some data for save to postgresql db

https://github.com/MiYogurt/flower_crawler/blob/master/flower_crawler/DB.fs

Expected behavior

let it work in mac os

Actual behavior

not work

Known workarounds

not found

https://github.com/fsprojects/SQLProvider/tree/master/tests/SqlProvider.Core.Tests/Postgres not work

Related information

➜  flower_crawler git:(master) mono --version
Mono JIT compiler version 5.18.1.3 (2018-08/fdb26b0a445 Wed Mar 20 10:02:02 EDT 2019)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
    TLS:
    SIGSEGV:       altstack
    Notification:  kqueue
    Architecture:  amd64
    Disabled:      none
    Misc:          softdebug
    Interpreter:   yes
    LLVM:          yes(600)
    Suspend:       preemptive
    GC:            sgen (concurrent by default)

➜  flower_crawler git:(master) dotnet --version
2.2.105
Thorium commented 5 years ago

It does. You need to add ResolutionPath parameter to a path where are the npgsql.dll and the dll missing in the error: System.Threading.Tasks.Extensions.dll

Get them from Nuget and put a directory referenced by ResolutionPath. The reason for this is that the database driver is loaded via reflection, and .Net reflection cannot load directly from Nuget.

MiYogurt commented 5 years ago

@Thorium Thanks very very very very much

He finally worked.

#/bin/env sh

cp /Users/yugo/.nuget/packages/npgsql/4.0.6/lib/netstandard2.0/Npgsql.dll ./libs/Npgsql.dll 

cp /Users/yugo/.nuget/packages/system.threading.tasks.extensions/4.5.2/lib/netstandard2.0/System.Threading.Tasks.Extensions.dll ./libs

cp /Users/yugo/.nuget/packages/system.runtime.compilerservices.unsafe/4.5.2/lib/netstandard2.0/System.Runtime.CompilerServices.Unsafe.dll ./libs

cp /Users/yugo/.nuget/packages/system.memory/4.5.2/lib/netstandard2.0/System.Memory.dll ./libs
let [<Literal>] connString = "Host=localhost;Database=test_db;Username=bob;Password=bob"

let [<Literal>] dbVendor = Common.DatabaseProviderTypes.POSTGRESQL

type sql = SqlDataProvider<dbVendor,connString, "", "./libs" >

let runtimeConnectionStr = connString

let ctx = sql.GetDataContext(runtimeConnectionStr)