Application that targets .NET 4.8 does not support System.Data.SqlClient that targets .NET Standard 2.0

Tilak H N 0 Reputation points
2024-09-09T04:28:25.75+00:00

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:

  1. Create a class library that targets .NET Standard 2.0 (TestLibrary)
  2. In the TestLibrary, reference and use SqlClient (which targets .NET Standard 2.0) latest version (4.8.6) and create a function to query something from the database
  3. Build/publish the TestLibrary
  4. Create a console application (TestApp) with .NET 4.8 as target framework and use that class library
  5. 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?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,806 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 53,896 Reputation points
    2024-09-09T05:16:36.3833333+00:00

    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.


  2. 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 of TestApp, an error will be triggered.

    The following document explains this type of error in Microsoft.Data.SqlClient. You could refer to it to understand System.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-method

    If 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 in TestApp 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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.