Jonas Hammerschmidt
Jonas Hammerschmidt

Reputation: 145

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

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.

Upvotes: 9

Views: 20359

Answers (4)

Jonas Hammerschmidt
Jonas Hammerschmidt

Reputation: 145

The only workaround I found so far was to add a Microsoft.EntityFrameworkCore.SqlServer reference to the executable projects' properties file.

Not exactly elegant; but it works.

Upvotes: 2

Mawa
Mawa

Reputation: 26

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();

Upvotes: 1

Mohamed Hasan
Mohamed Hasan

Reputation: 237

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.

Upvotes: 0

Mats Gausdal
Mats Gausdal

Reputation: 447

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.

Upvotes: 5

Related Questions