Skip to content

Microsoft.Data.SqlClient is not supported on this platform #2030

Closed

Description

@Benknightdark

Issue Description

I have an ASP.NET Core MVC project targeting .NET 7 and a library project also targeting .NET 7. After referencing the library project in the MVC project and publishing it, I receive the following error when trying to use the DbContext:

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

Expected Behavior

I expected to be able to use the DbContext without any issues after referencing the .NET 7 library project.

Steps to Reproduce

  1. Create a new ASP.NET Core MVC project targeting .NET 7.
  2. Create a new class library project targeting .NET 7.
  3. Add a database context to the class library project using Microsoft.EntityFrameworkCore.SqlServer package.
  4. Add a reference to the class library project in the MVC project.
  5. Publish the MVC project and attempt to use the database context.

Additional Information

  • I have tried the solutions suggested in Microsoft's documentation.
  • I have also tried downgrading the class library project to target .NET 6 and that resolved the issue. However, I need to target .NET 7 for other reasons.
  • This is a severe issue as it prevents me from using a library project that targets .NET 7 and Microsoft.Data.SqlClient.
  • This issue did not occur when using .NET 6 in both projects.

Activity

  1. transferred this issue fromdotnet/efcoreon May 3, 2023
  2. JesLun commented on May 3, 2023

    @JesLun

    I have this issue as well and also had it on .net 6 when running it on Linux.

    I found a workaround which is to send the runtime identifier. Like this dotnet run -r linux-x64.

  3. Benknightdark commented on May 4, 2023

    @Benknightdark
    Author

    Any solutions in windows environment?

  4. JesperLundberg commented on May 4, 2023

    @JesperLundberg

    You'd need to find the runtime id for the windows you're using.

    I found the Linux one somewhere in Microsofts documentation. Sorry that I can't be more specific.

    Following the same logic as the Linux one it'd be Windows-x64 but I'm just guessing here.

  5. SudoCerb commented on May 5, 2023

    @SudoCerb

    Microsoft's documentation that is linked by OP leads to a 404.

    Great.

    I have a similar issue. Creating it now and linking to this.

    I'm on Windows 11.

  6. JRahnama commented on May 5, 2023

    @JRahnama
    Contributor

    Could any one here provide a simple repro of the issue?

  7. SudoCerb commented on May 5, 2023

    @SudoCerb

    Could any one here provide a simple repro of the issue?

    I'll happily do so - just need to create one. Will take a crack at this first thing in the morning.

  8. SudoCerb commented on May 5, 2023

    @SudoCerb

    Hi @JRahnama , I've created the simplest case I could -

    https://github.com/SudoCerb/manictime-microsoft.data.sqlclient-min-repro

    Instructions are in the README.md.

    I hope that this helps!

  9. Kaur-Parminder commented on May 10, 2023

    @Kaur-Parminder
    Contributor

    I tried .Net7 ASP.NetCore WebAPP with class library targetting .NET7 on Windows 10. I am not seeing the exception, I am attaching the sample.

    The steps I followed:

    Scaffold-DbContext "Server=localhost;Database=Northwind;Integrated Security=true;Encrypt=false;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

    • Created WebApp (MVC) targetting .Net7
    • Reference the class library to Web App project.
    • Added code to Controller to get data from DB.
      var context = new NorthwindContext(); var test = context.Employees.First();

    Sample.zip

  10. JesperLundberg commented on May 11, 2023

    @JesperLundberg

    Just to add to what I wrote earlier. The link where I found the RID for Linux is here.

  11. Kaur-Parminder commented on May 12, 2023

    @Kaur-Parminder
    Contributor

    I set up on Ubuntu 22.04 with the same project. I can see data being fetched. .Net SDK 7.0.5 Runtime 7.0.203.

    linuxtest

  12. 18 remaining items

  13. shonuff373 commented on May 16, 2024

    @shonuff373

    I also had this problem when upgrading from .NET 7.0 to .NET 8.0. Adding the nuget package <PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0" /> To the executing application csproj fixed it. Why I don't know.

    I'm running into the same issue for the same reason. I read on a few other forums that downgrading to 5.1.5 helped. But that hasn't worked for me.

  14. bh3605 commented on Oct 15, 2024

    @bh3605

    .net 8 here. Still doesn't work.

  15. Woudjee commented on Nov 12, 2024

    @Woudjee

    I HAVE A SOLUTION FOR WINDOWS PEOPLE:

    This guy is right:

    I also had this problem when upgrading from .NET 7.0 to .NET 8.0. Adding the nuget package <PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0" /> To the executing application csproj fixed it. Why I don't know.

    Why? I'm not sure, but I believe that it has something to do with our sql projects being class libs instead of applications, thereby messing up package references. I have had similar problems with different packages some times. It has something to do with how the project is build for production being different than the project being build for debugging purposes. When debugging, all dll's are copied to the right place, when creating a release, they don't. This is something that is supposed to be fixed on the Microsoft side, like @RobDaytona was complaning about. I can relate to what you're writing very well man.

    So to solve these and similar issues: make sure that you reference the missing dll in your calling project, and most likely you're good to go. (I used the latest version).

  16. mavToday commented on Mar 6, 2025

    @mavToday

    The Package Microsoft.Data.SqlClient has a Reference to Microsoft.Data.SqlClient.SNI.runtime. This Packages includes a native assembly, that is necessary for the SqlClient with net8.0.

    However, if this native assembly is not present relative to the application (e.g. runtimes\win-x64\native), it will result in the "Microsoft.Data.SqlClient is not supported on this platform" exception.

    We put our NuGet-Packages not next to our application, but in a separate folder with our own AssemblyLoadContext. We can load the Microsoft.Data.SqlClient.dll Assembly, and even the native Microsoft.Data.SqlClient.SNI.dll via AssemblyLoadContext.LoadUnmanagedDll, but still resulting in the error. If we move them to the outputfolder for the application (oder unit-test), it works. The crazy thing is, that i can access the imported functions and call them correctly like in SniNativeMethods.cs

    For me it looks like, the resolving for the native assembly is buggy. But i dont find any related code in the repository.

  17. NavirBox commented on Oct 5, 2025

    @NavirBox

    No way this is closed. It's still happening with 9.0

  18. NintendoPlayer commented on Feb 20, 2026

    @NintendoPlayer

    Hi! Same problem here. It's driving me crazy, I don't know how to solve this issue. Everything was working in my project, had to update System.Data.Sqlclient because it was deprecated and the this nonesense started. Now going back to System. Data.Sqlclient and it shows me the same message.

    In my case I have a main project for my app and then several projects that compile as libraries. Almost all projects reference the nuget. Then I have a visual studio setup project that generates the installer. Everything works properly, installs the app but throws the error. I don't really don't know what I have to do in my solution to really solve the issue.

  19. IzharAzati commented on Feb 20, 2026

    @IzharAzati

    Try to revert to the oldest version of System.Diagnostics
    I had a similar problem when I built a DLL as an ADDIN for a graphics program (AutoCAD).
    Another way, if you have a project/projects with Multi Target, create projects for each Target.

  20. prlcutting commented on Feb 23, 2026

    @prlcutting

    I'm running into this problem too. We have a library project whose assembly is resolved dynamically at runtime (like a plugin) into the main application, and it's this library that has a dependency on Microsoft.Data.SqlClient. I have confirmed that Microsoft.Data.SqlClient.dll is copied to the correct location for the hosting .NET application, and that the native runtimes/win-x64/native/Microsoft.Data.SqlClient.SNI.dll dependency exists too. But no amount of cajoling will get this thing to work without throwing a PlatformNotSupportedException. The only recourse is adding an artificial and unwanted dependency from the main application's project to Microsoft.Data.SqlClient. But why? Why does this work? What is this actually fixing? Why can't Microsoft.Data.SqlClient resolve its native dependencies like our other NuGet packages that have native dependencies? Those work fine, without a hack to add them to the main application as a direct dependency.

    Since this issue is closed (despite continuing community feedback about recurring problems), will these comments even be observed by anybody at Microsoft who can perhaps look into it?

  21. AaronAmberman commented on Feb 25, 2026

    @AaronAmberman

    I have just now run into this problem too. I wanted to add my insight from my investigations in case they help point the community in the right direction.

    Generally, in NuGet packages, the DLL in question we seek is located in the lib directory in a target framework moniker (TFM) sub-directory (.net6.0, .net7.0-windows, .net8.0-windows10.0.26100, etc.) inside your package. I think this is not true for this particular Microsoft.Data.SqlClient NuGet package. The package is found in the runtimes sub-directory, more specifically runtimes/win/lib/TFM (in my case runtimes/win/lib/net8.0).

    If you were to install this package into a project via nuget.exe then you will see several copies of the Microsoft.Data.SqlClient.dll inside the downloaded resource. 1 in the aforementioned lib sub-directory (Microsoft.Data.SqlClient.6.1.4/lib/net8.0/Microsoft.Data.SqliClient.dll) which is not the one you want but is typically the one that NuGet finds if you are not a windows executable. If you were to open this one in any decompilation program (like dotPeek) you will see every declaration in every type simply throws a PlatformNotSupportException. They don't look at anything or inspect any parameters. 100% of everything just blindly throws a PNSE. This DLL in .NET8 is a little over 900KB.

    Now the other DLL, in runtimes, is the one we want. This is the one that contains the goods (has the actual code and doesn't just throw PNSEs everywhere). So this DLL, (Microsoft.Data.SqlClient.6.1.4/runtimes/win/lib/net8.0/Microsoft.Data.SqliClient.dll), is the one that is typically found when we reference this NuGet package in a WinExe application. I think that is because of the EXE versus LIBRARY project types in terms of how NuGet.exe (and the newer dotnet variants) does resource lookup. This DLL in .NET8 is about 2.1MB. Big difference from the one that just throws a bunch of exceptions.

    So if you reference this NuGet package in a DLL project it might never work right if MS doesn't change how the package is put together or how resource look ups work for DLL type projects within NuGet.

    I am not a guru and I definitely don't understand this stuff at a low level. Just trying to add some insight as to why a lot us seem to be seeing this PlatformNotSupportException. Hope this helps someone out there. :)

  22. Woudjee commented on Feb 25, 2026

    @Woudjee

    I have just now run into this problem too. I wanted to add my insight from my investigations in case they help point the community in the right direction.

    Generally, in NuGet packages, the DLL in question we seek is located in the lib directory in a target framework moniker (TFM) sub-directory (.net6.0, .net7.0-windows, .net8.0-windows10.0.26100, etc.) inside your package. I think this is not true for this particular Microsoft.Data.SqlClient NuGet package. The package is found in the runtimes sub-directory, more specifically runtimes/win/lib/TFM (in my case runtimes/win/lib/net8.0).

    If you were to install this package into a project via nuget.exe then you will see several copies of the Microsoft.Data.SqlClient.dll inside the downloaded resource. 1 in the aforementioned lib sub-directory (Microsoft.Data.SqlClient.6.1.4/lib/net8.0/Microsoft.Data.SqliClient.dll) which is not the one you want but is typically the one that NuGet finds if you are not a windows executable. If you were to open this one in any decompilation program (like dotPeek) you will see every declaration in every type simply throws a PlatformNotSupportException. They don't look at anything or inspect any parameters. 100% of everything just blindly throws a PNSE. This DLL in .NET8 is a little over 900KB.

    Now the other DLL, in runtimes, is the one we want. This is the one that contains the goods (has the actual code and doesn't just throw PNSEs everywhere). So this DLL, (Microsoft.Data.SqlClient.6.1.4/runtimes/win/lib/net8.0/Microsoft.Data.SqliClient.dll), is the one that is typically found when we reference this NuGet package in a WinExe application. I think that is because of the EXE versus LIBRARY project types in terms of how NuGet.exe (and the newer dotnet variants) does resource lookup. This DLL in .NET8 is about 2.1MB. Big difference from the one that just throws a bunch of exceptions.

    So if you reference this NuGet package in a DLL project it might never work right if MS doesn't change how the package is put together or how resource look ups work for DLL type projects within NuGet.

    I am not a guru and I definitely don't understand this stuff at a low level. Just trying to add some insight as to why a lot us seem to be seeing this PlatformNotSupportException. Hope this helps someone out there. :)

    Well done researching this. Your conclusions seem very reasonable. It’s the perfect substantiation for the message I posted here a while back. 👍 I’m happy I understand this slightly better now.

  23. bootrider commented on Jul 23, 2026

    @bootrider

    It is still happening in the version 7.0.1.

  24. bootrider commented on Jul 23, 2026

    @bootrider

    this solve my issue:
    Making the upgrade from .NET8 to .NET10 I had:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net10.0-windows</TargetFramework>
        <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
        <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
        <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
        <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
        <NoWarn>CA1416</NoWarn>
        <Platforms>x64</Platforms>
      </PropertyGroup>
    ...
    

    I changed to:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net10.0-windows</TargetFramework>
        <RuntimeIdentifier>win-x64</RuntimeIdentifier>     <!-- here is the detail -->
        <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
        <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
        <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
        <NoWarn>CA1416</NoWarn>
        <Platforms>x64</Platforms>
      </PropertyGroup>
    ...
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    External 🔗

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions