Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Benknightdark opened this issue May 3, 2023 · 26 comments
Closed

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

Benknightdark opened this issue May 3, 2023 · 26 comments
Labels
customer-reported 🔗 External Issue is in an external component

Comments

@Benknightdark
Copy link

Benknightdark commented May 3, 2023

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.
@ajcvickers ajcvickers transferred this issue from dotnet/efcore May 3, 2023
@JesLun
Copy link

JesLun commented May 3, 2023

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.

@Benknightdark
Copy link
Author

Any solutions in windows environment?

@JesperLundberg
Copy link

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.

@SudoCerb
Copy link

SudoCerb commented May 5, 2023

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.

@JRahnama
Copy link
Member

JRahnama commented May 5, 2023

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

@SudoCerb
Copy link

SudoCerb commented May 5, 2023

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.

@SudoCerb
Copy link

SudoCerb commented May 5, 2023

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!

@Kaur-Parminder
Copy link
Contributor

Kaur-Parminder commented May 10, 2023

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:

  • Create Database using script https://github.com/dotnet/SqlClient/blob/main/tools/testsql/createNorthwindDb.sql
  • Create Class Library .Net7. and reference following packages
    <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.5" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.5"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> </ItemGroup>
  • Run command below to create DBContext for Northwind DB created above

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

@JesperLundberg
Copy link

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

@Kaur-Parminder
Copy link
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

@SimonGeering
Copy link

Just wanted to add that I'm getting the same exception when init dbcontext, .net 7 (latest sdk) on Linux mint 21 running in blazer server with a DAL in a separate DLL project.

Running the blazer solution on the metal and connecting to a SQL 2022 instance in a container.

Exactly the same code works fine on two separate windows 10 enterprise boxes, connecting native and container SQL server 2022 respectively.

Repo is opensource here, and reproducible from the PlatformNotSupportedException branch https://github.com/SimonGeering/AdminAssistant/tree/PlatformNotSupportedException

I'll try to get the time this week to add repo instructions but give it the config for the SQL server and run an ef migration and boom.

Happy to help with testing if any fix can come for this, will also try it with the 8 preview when I get the chance and let you know.

@Kaur-Parminder Kaur-Parminder added the 🔗 External Issue is in an external component label Jul 11, 2023
@DavoudEshtehari
Copy link
Member

Detecting the correct target framework is on runtime to be discovered not MDS. Any failed attempts will fall back to the "not supported platform" dll and throw this exception which is by design.

@SudoCerb
Copy link

Detecting the correct target framework is on runtime to be discovered not MDS. Any failed attempts will fall back to the "not supported platform" dll and throw this exception which is by design.

I think it would be quite useful for the error to say something like that. I have no idea how to get around this but that error message would have been more helpful!

@derek-mba
Copy link

In my case, I was compiling just fine yesterday; moved to a new account on the same server (also Linux Mint 21) today and even though I'm targetting dotnet6 and have only dotnet 6 runtimes and SDK installed, I'm getting the same.

@derek-mba
Copy link

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]

@DanwithArduino
Copy link

DanwithArduino commented Sep 6, 2023

I'm having this issue as well now. All packages and dotnet SDK on my 20.04 server are up to date. Builds fine locally, but fails on server deploy. Dotnet 7.

edit: weird. It doesn't appear the microsoft.data.sqlclient has not been updated between my previous update and my current update - both on 5.1.1................

edit: Setting self contained to false resolved the issue. The post here: #1643 helped with that trouble shooting step.

@dungarDot
Copy link

dungarDot commented Oct 6, 2023

For what it's worth I just started having failures on this with a repo that worked two weeks ago. I had to re clone down the repo, made some code changes, and now it doesn't work. Windows 10, net7.0. Setting self contained to false has not helped.

edit: Creating a brand new publish profile from scratch and basically not touching it fixed it. But it's identical to the profile that doesn't function.

@DavoudEshtehari
Copy link
Member

Closing it as it's external. Feel free to post in this thread.

@joaopf-silva
Copy link

Did someone manage to get this working for Windows?

Windows 11, Visual Studio 2022, .NET Standard 2.0 Class Library project

@dragos-durlut
Copy link

For Docker DOTNET Alpine I had to set the runtime id to linux-musl-x64

Previously I had only the architecture set to amd64.

FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build

RUN dotnet publish -r linux-musl-x64 "path.to.csproj" -c Release -o /app \
    --self-contained true \
    /p:PublishSingleFile=false
    
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0-alpine AS final

ENTRYPOINT ["./entry-point-file"]

@RobDaytona
Copy link

RobDaytona commented Apr 13, 2024

I just don't understand how any one is supposed to develop using Microsoft .net technologies any more. The stuff is so buggy it drives me insane. Yesterday my API was working perfectly accessing stored procs no problems, today first thing it says is: Microsoft.Data.SqlClient is not supported on this platform
#2030 and the only thing that has changed is I implemented bearer token security into the api call? so no mods whatsover with sql sever.. or anything database related and I am getting "Microsoft.Data.SqlClient is not supported on this platform" - wtf I have an app to write and I am constantly running into issues all the time. does my head in. I have basic bugs open in .net Maui, that get verified as bugs, and previously xamarin, I report the bug, the bug gets verified, then never fixed.. this is ridiculous. Does any one have any ideas what is causing this issue? and or recommendations on what to fix, this thread is very confusing with people on about linux etc.. my situation is .net8, .net aspcore api project (or whatever its called these days).. running on a dedicated API dns on a windows 2016 server.. so where is the issue? is it the api project ? or is it the server, and why did this work yesterday and today after implementing token bearer security on the api call it does this? its madness.. aaaaargg!!! I am ging to revert to a back up from yesterday that does not include any of the token/bearer security code I implemented today to see if it suddenly works? I can't see why token bearer security would have anything to do with this error message.. aaargg!

@RobDaytona
Copy link

RobDaytona commented Apr 13, 2024

Update: I reverted to yesterdays backup of the API and Client, deployed the API to the win 2016 server and it successfully calls the database strored procs with out this stupid error. As I said the only thing I did was add Token Bearer API security to the API and Client and this error suddenly popped up.. "Microsoft.Data.SqlClient is not supported on this platform" I am going to try repeat these steps and see if it does it again. I hope not!

@RobDaytona
Copy link

Error no longer appears, when I reworked my steps from yesterday's backup thank goodness. Who knows what happened first time.?

@IzharAzati
Copy link

In my case, adding to .csproj file:

<!--see: https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#copylocallockfileassemblies  -->
**<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>**
<!--<ProduceReferenceAssemblyInOutDir>true</ProduceReferenceAssemblyInOutDir>-->
<!--<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>-->
**<RuntimeIdentifier>win-x64</RuntimeIdentifier>**

managed to make it work.
(SQL, ODBC, ...)
In my case, I'm developing a DLL plugin for the AutoCAD software, the problem is that all Microsoft's tests / examples are for EXE applications and not for DLLs.

@johnricketts
Copy link

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.

@shonuff373
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customer-reported 🔗 External Issue is in an external component
Projects
Development

No branches or pull requests