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

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.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 2
  • Comments: 18 (5 by maintainers)

Most upvoted comments

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.

Any solutions in windows environment?

On Mint/Ubuntu it seems to be an issue with the Ubuntu versions of Dotnet. I replaced them with the Microsoft packages from https://packages.microsoft.com/ubuntu/22.04/prod and all is now fine. [Instructions at https://learn.microsoft.com/en-ca/dotnet/core/install/linux-ubuntu#register-the-microsoft-package-repository]

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!