Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function - System.Data.SqlClient is not supported on this platform

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?

like image 751
Alex Gordon Avatar asked Dec 28 '18 00:12

Alex Gordon


2 Answers

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.

like image 121
Jerry Liu Avatar answered Oct 02 '22 14:10

Jerry Liu


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

like image 26
chathura prasad amarathunga Avatar answered Oct 02 '22 14:10

chathura prasad amarathunga