简体   繁体   English

F#将SQLProvider与Postgres连接

[英]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: 我是F#和.Net世界的初学者,我正在尝试使这个F#脚本工作:

#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. 错误FS3033:类型提供程序'FSharp.Data.Sql.SqlTypeProvider'报告错误:无法解析字段标记0x04000523,原因是:无法加载字段类型'Npgsql.NpgsqlConnection + d__28:<> u__2'(7)到期to:无法加载文件或程序集'System.Threading.Tasks.Extensions,Version = 4.1.0.0,Culture = neutral,PublicKeyToken = cc7b13ffcd2ddd51'或其依赖项之一。 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: 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类型:d__28成员:(null)签名:

Which is weird, since Npgsql is actually imported (so the last #r actually works). 这很奇怪,因为实际导入了Npgsql (所以最后#r实际上是有效的)。

The database is up via docker: 数据库通过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. 此外,该项目是通过.Net Core 2.1.200生成的,并且在Linux上运行。

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. 这是因为类型提供程序需要解析第三方驱动程序的依赖关系(在本例中为Npgsql ),并且它不能,因为它无权访问像NuGet(或Paket)那样的完整依赖关系树。 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. 您需要将System.Threading.Tasks.Extensions.dll复制到Npgsql.dll所在的同一文件夹(您的ResPath ),以便类型提供程序可以解析该程序集。

Once you do that, you'll find that you also need to copy System.Runtime.CompilerServices.Unsafe . 一旦你这样做,你会发现你还需要复制System.Runtime.CompilerServices.Unsafe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM