.NET 7 Microsoft.Data.SqlClient PlatformNotSupportedException error on DbContext use

136 views Asked by At

I have migrated my class library project from .NET Framework 4.8 to .NET 7 using the upgrade assistant. My SQL Server database is in the cloud. I am working on Windows 10 OS and with Visual Studio 2022.

When I try to use the DbContext I get this error:

System.PlatformNotSupportedException: 'Microsoft.Data.SqlClient is not supported on this platform.'

PackageReferences are listed below (using NuGet to add them to the project):

<ItemGroup>
  <PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.5" />
  <PackageReference Include="Microsoft.Data.SqlClient.SNI.runtime" Version="5.1.1" />
  <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.15" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.15" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.15">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  </PackageReference>
  <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.15" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.15" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.6" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.15">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  </PackageReference>
  <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
  <PackageReference Include="Microsoft.SqlServer.Server" Version="1.0.0" />
  <PackageReference Include="PreEmptive.Protection.Checks.Attributes" Version="2.0.0" />
  <PackageReference Include="System.Data.Common" Version="4.3.0" />
  <PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
  <PackageReference Include="System.Interactive.Async" Version="6.0.1" />
  <PackageReference Include="System.Memory" Version="4.5.5" />
  <PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
  <PackageReference Include="System.Reflection.TypeExtensions" Version="4.7.0" />
  <PackageReference Include="System.Runtime.Handles" Version="4.3.0" />
  <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
  <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
  <PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
  <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
  <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
  <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
  <PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
  <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
  <PackageReference Include="Microsoft.Extensions.Options" Version="8.0.1" />
  <PackageReference Include="Microsoft.Extensions.Primitives" Version="8.0.0" />
  <PackageReference Include="Microsoft.NETCore.Platforms" Version="7.0.4" />
  <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  <PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
  <PackageReference Include="System.ComponentModel.Composition" Version="8.0.0" />
  <PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
  <PackageReference Include="System.Data.OleDb" Version="8.0.0" />
  <PackageReference Include="System.Diagnostics.DiagnosticSource" Version="8.0.0" />
  <PackageReference Include="System.Management" Version="8.0.0" />
  <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
  <PackageReference Include="System.Security.Cryptography.Cng" Version="5.0.0" />
  <PackageReference Include="System.Security.Cryptography.Pkcs" Version="8.0.0" />
  <PackageReference Include="System.Security.Cryptography.Xml" Version="7.0.1" />
</ItemGroup>

DbContext code looks like this:

public partial class dbContext : DbContext
{
    public dbContext()
        : base(new DbContextOptionsBuilder<dbContext>().UseSqlServer("Data Source=SERVER_IP;Initial Catalog=MY_DB_NAME;User Id=USER_NAME;Password=MY_PW;").Options)
    {
    }
}

I have tried to downgrade dependencies but it didn't work.

I've also tried to specify runtime in the .csproj file like below, but that also did not work:

<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<RuntimeIdentifier>win</RuntimeIdentifier>
  <ItemGroup>
    <None Include="$(USERPROFILE)\.nuget\packages\microsoft.data.sqlclient\5.1.5\runtimes\win\lib\net6.0\Microsoft.Data.SqlClient.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
  <Target Name="CopyToBin" BeforeTargets="Build">
    <Copy SourceFiles="$(USERPROFILE)\.nuget\packages\microsoft.data.sqlclient\5.1.5\runtimes\win\lib\net6.0\Microsoft.Data.SqlClient.dll" DestinationFolder="$(OutputPath)\bin" />
  </Target>

I have also cleared the nuget cache but it didn't help. I guess I am in problem with package versions but not sure which versions to use. All of them seems to be the latest versions.

1

There are 1 answers

0
Thomas Phaneuf On

Brute force approach (that works)

I have found that sometimes adding Microsoft.Data.SqlClient to all the projects involved (even if it doesn't seem like they would need them) solves the problem. Then once everything compiles, you can go in and try removing them one at time and recompiling until only the ones you need remain.