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

4

8

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.

Esoteric answered 22/6, 2020 at 12:43 Comment(3)
refer URLs #49356030 may help youJanus
No, they are facing a different problem, which produces a similar error.Esoteric
Hi @JonasHammerschmidt How did you solved this problem? We are also facing the same issue when an app loads our DLL using Assembly.LoadFrom()Nippur
F
5

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.
Following answered 21/4, 2021 at 11:21 Comment(0)
E
2

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.

Esoteric answered 23/6, 2020 at 20:58 Comment(2)
Are you keeping or removing the Microsoft.Data.SqlClient dependency? I've updated everything to .Net5.0 and SqlClient seems to be the one thing that isn't yet updated.Venial
This also worked for me - I removed it as I was using POSTGreSQL and thought it not needed. Apparently it is...Witticism
F
1

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();
Franz answered 30/6, 2022 at 1:38 Comment(1)
Switching to a different driver that is no longer actively maintained is not so much a solution as a workaround.Bartender
H
0

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.

Handknit answered 19/6, 2021 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.