15

I'm running the following insert code within my azure function into an azure sql server 2014 database:

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

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

and getting the following exception:

System.Data.SqlClient is not supported on this platform

Here are the chains of dependencies it's using:

enter image description here

How do I run a simple sql command from my app? What am I doing wrong?

Jerry Liu
  • 17,282
  • 4
  • 40
  • 61
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • Same problem for me on production environment with Windows Server 2016, .NET Core 2.1.6. I don't have any direct reference to nuget package of System.Data.SqlClient v4.6.0. And so I don't know how that have been updated and also how to remove or revert it... – Cheshire Cat Jan 11 '19 at 16:56

5 Answers5

21

If you don't need the latest stable version 4.6.0, simply revert to 4.5.1 would work.

Otherwise the workaround is to load the assemblies on our own. Right click on Function project and Edit <FunctionAppName>.csproj, add items below to copy related assemblies to output dir.

  <!-- 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>

There's an issue tracking this assembly reference problem.

Jerry Liu
  • 17,282
  • 4
  • 40
  • 61
4

I have downgrade the System.Data.SqlClient to 4.6.0 then it worked for me

3

I just ran into this with an Azure Functions instance that had somehow been configured to use .NET Core 3.0.0. I changed the configuration setting FUNCTIONS_EXTENSION_VERSION from beta to ~2. After I restarted it, it went back to using 2.0.x and this error went away.

mdickin
  • 2,365
  • 21
  • 27
3

in my case i got resolved this by changing runtime version 3 to 2 under FunctionAppSettings in Azure's FunctionApp

0

If you're using EF Core, v2.1.4 seems to work.

Mike Dean
  • 67
  • 1
  • 3