使用F# SQLProvider连接MS SQL Server出现System.Data.SqlClient报错如何解决
F# SQLProvider连接MSSQL报错System.Data.SqlClient不支持当前平台
我尝试使用适配F#的SQLProvider连接MS SQL Server,按照推荐配置操作后无法正常运行,我的模块代码如下:
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>
添加上述代码后,智能提示首先弹出如下报错信息:
类型提供程序'FSharp.Data.Sql.SqlTypeProvider'报告错误: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仓库提交Issue?欢迎各位提供解决思路和建议,谢谢!
我当前使用的环境版本如下:
- .NET 6
- VS Code (1.62.2)
- Ionide (v5.9.0)
- SQLProvider Version 1.2.10
- System.Data.SqlClient Version 4.8.3
解决方案
这个报错是因为.NET Core/.NET 5+环境下,旧的System.Data.SqlClient已经被微软废弃,跨平台兼容性支持终止,SQLProvider默认使用旧驱动就会触发平台不支持错误,不需要提交Issue,修改配置即可解决,步骤如下:
- 替换NuGet依赖:卸载项目中的
System.Data.SqlClient包,安装最新稳定版的Microsoft.Data.SqlClient包。 - 修改
SqlDataProvider的静态参数,新增SqlClientLibrary配置指定使用新驱动:
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 // 新增配置:指定使用Microsoft.Data.SqlClient驱动 [<Literal>] let sqlClientLib = Common.SqlClientLibrary.MicrosoftDataSqlClient type sql = SqlDataProvider< dbVendor, connString, IndividualsAmount=indivAmount, UseOptionTypes=useOptTypes, SqlClientLibrary = sqlClientLib >
- 如仍有报错,在项目
.fsproj文件的PropertyGroup节点中添加以下配置,禁用隐式引用旧SqlClient:
<DisableImplicitSystemDataSqlClient>true</DisableImplicitSystemDataSqlClient>
- 重启VS Code的Ionide语言服务(或直接重启VS Code)清除类型提供程序缓存即可。
内容的提问来源于stack exchange,提问作者CoderLee
相关产品推荐
相关产品推荐

