如何使用 F# 和 SQLProvider 连接到 MS SQL 服务器?

How to connect to MS SQL Server with F# with SQLProvider?

我正在尝试使用 SQLProvider for MS SQL Server with F#,但似乎无法使用推荐的设置。

查看下面我的模块:

namespace MyApp

open FSharp.Data.Sql

module Database =

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


    [<Literal>]
    let connString =
        "Data Source=localhost;Database=MyDatabase;User Id=user;Password='my-password-here'"

    [<Literal>]
    let indivAmount = 1000

    [<Literal>]
    let useOptTypes = true

    type sql = SqlDataProvider<dbVendor, connString, IndividualsAmount=indivAmount, UseOptionTypes=useOptTypes>

我的主要问题是,在添加这段代码后,我在智能感知中得到了这个message/warning

The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: System.Data.SqlClient is not supported on this platform.F# Compiler(3033)

尝试编译时出现构建错误:

Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
C:\Development\F#\MyApp\DataTypes.fs(21,16): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: System.Data.SqlClient is not supported on this platform. [C:\Development\F#\MyApp\MyApp.fsproj]
C:\Development\F#\MyApp\DataTypes.fs(21,16): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: System.Data.SqlClient is not supported on this platform. [C:\Development\F#\MyApp\MyApp.fsproj]

Build FAILED.

C:\Development\F#\MyApp\DataTypes.fs(21,16): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: System.Data.SqlClient is not supported on this platform. [C:\Development\F#\MyApp\MyApp.fsproj]
C:\Development\F#\MyApp\DataTypes.fs(21,16): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: System.Data.SqlClient is not supported on this platform. [C:\Development\F#\MyApp\MyApp.fsproj]
    0 Warning(s)
    2 Error(s)

Time Elapsed 00:00:06.21

我是不是做错了什么,或者这真的无法将默认设置与最新的 .NET 和 F# 一起使用,我需要提交一个 git 问题?欢迎任何见解和批评,谢谢!

发帖时正在使用:

运行 来自命令行: dotnet add package microsoft.data.sqlclient

然后将 Common.DatabaseProviderTypes.MSSQLSERVER 更改为 Common.DatabaseProviderTypes.MSSQLSERVER_DYNAMIC

为了解决这个问题,我在 Visual Studio (v17 2022) 中打开了项目并执行了以下操作:

  • 删除了依赖项
  • 在 SQLProvider 中重新添加并且 System.Data.SqlClient (v4.8.3)
  • 清除了 bin 和 obj 文件夹

完成上述操作后,我能够 运行 应用程序并连接到数据库,然后进行简单的 select 查询,返回预期数据。

不知道为什么,但 Ionide 总是给出可怕的 红色波浪形 ,即使构建得很好。