I cannot replicate your issue. I suspect it has something to do with the actual SQL code you're using and not NET Standard but that is just a guess at this point. Please provide a link to a minimally reproducible code sample that we can look at.
Application that targets .NET 4.8 does not support System.Data.SqlClient that targets .NET Standard 2.0
I have a dll (say TestLib.dll
) that targets .NET Standard 2.0, which uses System.Data.SqlClient
dll (this also targets .NET Standard 2.0).
Now I'm using the TestLib.dll
in an application (say TestApp
) that targets .NET 4.8. Running the application results in an exception
System.Data.SqlClient is not supported on this platform
It is because TestApp
was loading the System.Data.SqlClient.dll
that targets .NET Standard?
If I give the reference to the the System.Data.SqlClient.dll
that targets .NET 4.6.1, it works.
To reproduce the issue:
- Create a class library that targets .NET Standard 2.0 (
TestLibrary
) - In the
TestLibrary
, reference and useSqlClient
(which targets .NET Standard 2.0) latest version (4.8.6) and create a function to query something from the database - Build/publish the
TestLibrary
- Create a console application (
TestApp
) with .NET 4.8 as target framework and use that class library - Invoke the function that is querying the database
Note: make sure TestApp
is referencing SqlClient
that targets .NET Standard 2.0.
When it tries to create a connection object, the exception occurs.
What is the reason for .NET 4.8 not supporting the SqlClient
dll that targets .NET Standard 2.0?
2 answers
Sort by: Most helpful
-
-
Hongrui Yu-MSFT 1,605 Reputation points Microsoft Vendor
2024-09-09T07:29:34.0833333+00:00 Hi,@Tilak H N. Welcome to Microsoft Q&A.
From the current test results, it could be seen that when the framework of
TestLibrary
is different from that ofTestApp
, an error will be triggered.The following document explains this type of error in
Microsoft.Data.SqlClient
. You could refer to it to understandSystem.Data.SqlClient
: https://github.com/dotnet/SqlClient/wiki/Frequently-Asked-Questions#11-why-do-i-get-a-platformnotsupported-exception-when-my-application-hits-a-sqlclient-methodIf you want
TestLibrary
to maintain.NET Standard 2.0
,TestApp
to maintain.NET Framework 4.8
and the program to run normally, you could refer to the following solution.Solution: Add
System.Data.SqlClient.dll
inTestApp
through NuGet.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.