如何使用带有 Entity Framework 6 的 Azure 函数 V3(异常 System.Data.SqlClient:此平台不支持 System.Data.SqlClient)?

How to use Azure function V3 with Entity Framework 6 (exception System.Data.SqlClient: System.Data.SqlClient is not supported on this platform)?

我有一个必须调用基于 Entity Framework 6.4 构建的 DAL 存储库的 Azure V3 应用程序 在数据库初始化时出现异常:

System.Data.SqlClient: System.Data.SqlClient is not supported on this platform

来自:System.Data.SqlClient 4.8

public sealed partial class SqlConnection : System.Data.Common.DbConnection, System.ICloneable
{
    public SqlConnection() 
    { 
        throw new System.PlatformNotSupportedException(System.SR.PlatformNotSupported_DataSqlClient)
    }
}

在类似的线程中,解决方案是将 System.Data.SqlClient 降级到版本 4.5.1,但在我的例子中,依赖项是由 Entity Framework 添加的。

有人知道是否可以在 Azure Function V3 应用程序中使用 Entity Framework 6.4 吗?

看来还是.NET Core 3.0 SDK的bug,下面是workaround你可以参考

右键单击函数项目并编辑<FunctionAppName>.csproj,在下面添加项目以将相关程序集复制到输出目录。

<ItemGroup>
    <None Include="$(USERPROFILE)\.nuget\packages\system.data.sqlclient.6.0\runtimes\win\lib\netcoreapp2.1\System.Data.SqlClient.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
</ItemGroup>

如果您仍想使用 .NET Core 3.0 SDK,或者还需要 Functions v3 的此修复程序:

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <!-- https://github.com/Azure/azure-functions-host/issues/3568#issuecomment-461182536 -->
    <Exec Command="copy $(OutDir)$(ProjectName).deps.json $(OutDir)bin\function.deps.json" />
  </Target>
  <Target Name="PostPublish" BeforeTargets="Publish">
    <!-- https://github.com/Azure/azure-functions-host/issues/3568#issuecomment-461182536 -->
    <Exec Command="copy $(PublishDir)$(ProjectName).deps.json $(PublishDir)bin\function.deps.json" />
    <!-- https://github.com/Azure/azure-functions-vs-build-sdk/issues/333 -->
    <Exec Command="move $(PublishDir)\runtimes $(PublishDir)\bin" />
  </Target>