Microsoft.Data.SqlClient is not supported on this platform - Entity Framework Core 3.1

Viewed 9401

I'm using Microsoft.EntityFrameworkCore.SqlServer (3.1) in a .NET Core 3.1 library. This library gets loaded at runtime by an executable .NET Core project by using:

Assembly.LoadFrom('some.dll');

When trying to retrieve data from a DbSet, I get the following exception:

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

I guess it has something to do with loading the library at runtime, but I don't get why?

I tried various different things, like overriding the Microsoft.Data.SqlClient library with Version 1.1 or 2.0, but without any success.

4 Answers

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

My solution:

  • Add the latest version of Microsoft.Data.SqlClient as a NuGet dependency (Current v.2.1.2)
  • If you are using the Newtonsoft namespace, e.g. Newtonsoft.Json and this namespace could not be found after installing Microsoft.Data.SqlClient, then get the Newtonsoft.Json dependency from NuGet.

The only workaround I found so far was to add 'Microsoft.EntityFrameworkCore.SqlServer' to the executable project. Not elegant, but it works.

I had the same problem, it was solved by simply re-uploading the bin folder (not only the .dll file) again to the production server.

I solved that by using System.Data.SqlClient package, and pass the costom SqlConnection to dbContext as the following example:

    //this ConnectionString for database in sqlserver
var ConnectionString = @"Server=tcp:192.168.1.102,1433;Initial Catalog=db_name`enter code here`;Persist Security Info=False;User ID=test;Password=test;MultipleActiveResultSets=True;Connect Timeout=50;Encrypt=False;TrustServerCertificate=False";

//Creat Connection with sqlClient in System.Data.SqlClient .dll
System.Data.SqlClient.SqlConnection TestConnection = new System.Data.SqlClient.SqlConnection(ConnectionString);

//Create OptionBuilder
var optionsBuilder = new DbContextOptionsBuilder();

optionsBuilder.UseSqlServer(TestConnection);

//Create dbContext and pass connection builder that has sqlConnection
var dbContext = new AppDbContextMeClient(optionsBuilder);

//then call tables
var Students = dbContext.StudentsEntities.All();
Related