dotnet / SqlClient

Microsoft.Data.SqlClient provides database connectivity to SQL Server for .NET applications.
MIT License
821 stars 272 forks source link

PlatformNotSupportedException on DbContext use #2352

Closed Tahirhan closed 5 months ago

Tahirhan commented 5 months ago

I have migrated my class library project from .NET Framework 4.8 to .NET 7. My SQL server database is on the cloud. I am working on Windows 10 OS - Visual Studio 2022. When I try to use the DbContext I get the error message below:

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 block is like below:

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 csproj file like below but they did not work either.

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

JRahnama commented 5 months ago

@Tahirhan are you trying to use System.Data.SqlClient or Microsoft.Data.SqlClient? I can see below reference in your csproj

<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />

Tahirhan commented 5 months ago

Hi @JRahnama, Removed System.Data.SqlClient from dependencies but still the same error. Also, I tried with Microsoft.Data.SqlClient version 5.2.0-preview5.24024.3 and Microsoft.Data.SqlClient.SNI.runtime version 5.2.0-preview1.23340.1 but didn't help.

ErikEJ commented 5 months ago

@Tahirhan Please share full .csproj file content, you have way too many dependencies for a .NET 7 project

Tahirhan commented 5 months ago

@ErikEJ here is the full .csproj content, I have project references as well:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net7.0-windows</TargetFramework>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug32</Configuration>
    <OutputType>Library</OutputType>
    <IsWebBootstrapper>false</IsWebBootstrapper>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <UseWindowsForms>true</UseWindowsForms>
    <ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
    <OutputPath>bin\</OutputPath>
    <TargetExt>.rhp</TargetExt>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    <!--<RuntimeIdentifier>win-x64</RuntimeIdentifier>-->
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug32|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <Optimize>false</Optimize>
    <OutputPath>bin\</OutputPath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug64|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <Optimize>false</Optimize>
    <OutputPath>bin\</OutputPath>
    <CodeAnalysisIgnoreBuiltInRules>false</CodeAnalysisIgnoreBuiltInRules>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <OutputPath>bin\</OutputPath>
    <TargetExt>.rhp</TargetExt>
    <!--<InvariantGlobalization>false</InvariantGlobalization>-->
  </PropertyGroup>
  <ItemGroup>
    <Compile Update="Properties\Settings.Designer.cs">
      <DesignTimeSharedInput>True</DesignTimeSharedInput>
      <AutoGen>True</AutoGen>
      <DependentUpon>Settings.settings</DependentUpon>
    </Compile>
    <Compile Update="UserControl1.cs">
      <SubType>UserControl</SubType>
    </Compile>
  </ItemGroup>
  <ItemGroup>
    <None Update="Properties\Settings.settings">
      <Generator>SettingsSingleFileGenerator</Generator>
      <LastGenOutput>Settings.Designer.cs</LastGenOutput>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\..\DbConfigManager\DbConfigManager\DbConfigManager.csproj" />
    <ProjectReference Include="..\..\NotilusEventHandler\NotilusEventHandler\NotilusEventHandler.csproj" />
    <ProjectReference Include="..\..\NotilusSuiteInstaller\SuiteLicense\SuiteLicense.csproj" />
    <ProjectReference Include="..\..\QLicense-master\QLicense-master\Core\ActivationControls4Win\ActivationControls4Win.csproj" />
    <ProjectReference Include="..\..\QLicense-master\QLicense-master\Core\QLicense\QLicense.csproj" />
    <ProjectReference Include="..\ClipperLicense\ClipperLicense.csproj" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0-preview5.24024.3" />
    <PackageReference Include="Microsoft.Data.SqlClient.SNI.runtime" Version="5.2.0-preview1.23340.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.8" />
    <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.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.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>
      <ItemGroup>
    <Reference Include="ClipperManager">
      <HintPath>..\..\ClipperManager\ClipperManager\bin\Debug\ClipperManager.dll</HintPath>
    </Reference>
    <Reference Include="RhinoCommon">
      <HintPath>..\..\..\..\..\..\Program Files\Rhino 8\System\RhinoCommon.dll</HintPath>
      <Private>False</Private>
    </Reference>
  </ItemGroup>
  <!--<PropertyGroup>
    <PostBuildEvent>Copy "$(TargetPath)" "$(TargetDir)$(ProjectName).rhp"
</PostBuildEvent>
  </PropertyGroup>-->
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug32|AnyCPU' ">
    <StartArguments>
    </StartArguments>
    <StartAction>Program</StartAction>
    <StartProgram>C:\Program Files\Rhino 8\System\Rhino.exe</StartProgram>
  </PropertyGroup>
  <PropertyGroup>
    <FallbackCulture>en-US</FallbackCulture>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug64|AnyCPU'">
    <StartProgram>C:\Program Files\Rhino 8\System\Rhino.exe</StartProgram>
    <StartArguments>
    </StartArguments>
    <StartAction>Program</StartAction>
  </PropertyGroup>
  <!--<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>-->
</Project>
ErikEJ commented 5 months ago

You really need to clean up your package references, or start from scratch!

I think it can be reduced to something like this:

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.15" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.15">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="PreEmptive.Protection.Checks.Attributes" Version="2.0.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  </ItemGroup>
JRahnama commented 5 months ago

Closing as this is not a SqlClient issue.

codarthio commented 5 months ago

Hello, I am running into this exact issue as well. Using Microsoft.EntityFrameworkCore.SqlServer v7.0.14. Can you please reopen this?

JRahnama commented 5 months ago

@codarthio this is not a SqlClient issue. Can you provide a sample repro?

codarthio commented 5 months ago

@JRahnama yes you are right, it turned out to be an issue with an implicit dependency and how we had our service deployed. No further issues.

Thorium commented 1 month ago

What happens here is that you end-up loading the dll from Microsoft.Data.SqlClient Nuget-package lib-folder and that throws PlatformNotSupportedException meanwhile you should load a package from the runtimes folder. I have no idea how to affect the folder, even trying to manually load the correct file, you cannot, because the Assembly with same name is already loaded.