c# - Azure Function - 此平台不支持 System.Data.SqlClient

标签 c# azure .net-core azure-functions

我正在 azure 函数中运行以下插入代码到 azure sql server 2014 数据库中:

    private static void Command(SqlConnection sqlConnection, string query)
    {
        var sqlCommand = new SqlCommand(query, sqlConnection);

        try
        {
            sqlConnection.Open();
            sqlCommand.ExecuteNonQuery();
        }
        finally
        {
            sqlConnection?.Close();
        }
    }

并出现以下异常:

System.Data.SqlClient is not supported on this platform

以下是它使用的依赖链:

enter image description here

如何从我的应用程序运行简单的 sql 命令?我做错了什么?

最佳答案

如果不需要最新稳定版本 4.6.0 ,只需恢复为 4.5.1会起作用的。

否则,解决方法是自行加载程序集。右键单击 Function 项目并 Edit <FunctionAppName>.csproj ,添加以下项目以将相关程序集复制到输出目录。

  <!-- For publish -->
  <ItemGroup>
    <None Include="$(USERPROFILE)\.nuget\packages\system.data.sqlclient\4.6.0\runtimes\win\lib\netcoreapp2.1\System.Data.SqlClient.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
  <!-- For local debug -->
  <Target Name="CopyToBin" BeforeTargets="Build">
    <Copy SourceFiles="$(USERPROFILE)\.nuget\packages\system.data.sqlclient\4.6.0\runtimes\win\lib\netcoreapp2.1\System.Data.SqlClient.dll" DestinationFolder="$(OutputPath)\bin" />
  </Target>

有一个 issue跟踪此程序集引用问题。

关于c# - Azure Function - 此平台不支持 System.Data.SqlClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53952409/

相关文章:

c# - 是否在 LINQ 中使用 AsEnumerable()?

c# - Unity解析对象并在依赖解析对象中获取相同的对象

azure - 如何在Azure DevOps中使用docker构建任务

azure - 某些 Azure CLI 命令出现 SSL 握手错误

asp.net-core - .Net Core 中间件多次点击页面请求

c# - 在 C# 中使用 WMI 更改用户的家长控制设置

c# - 通过 Linq-to-entities 设置扩展属性

c# - 自定义指标遥测不会出现在 Application Insights 的指标浏览器中

.net - Dotnet Core Nuget 设置代理

c# - 如何从 ASP.NET Core 3.1 中的 appsettings.json 读取整个部分?