繁体   English   中英

此平台不支持 Microsoft.Data.SqlClient - Entity Framework Core 3.1

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

我在 .NET Core 3.1 库中使用Microsoft.EntityFrameworkCore.SqlServer (3.1)。 该库在运行时由可执行的 .NET Core 项目加载,方法是使用:

Assembly.LoadFrom('some.dll');

尝试从DbSet检索数据时,出现以下异常:

System.PlatformNotSupportedException:“此平台不支持 Microsoft.Data.SqlClient。”

我想这与在运行时加载库有关,但我不明白为什么?

我尝试了各种不同的东西,比如用 1.1 或 2.0 版本覆盖Microsoft.Data.SqlClient库,但没有任何成功。

我收到此消息: System.PlatformNotSupportedException: Microsoft.Data.SqlClient is not supported on this platform.

我的解决方案:

  • 添加最新版本的 Microsoft.Data.SqlClient 作为 NuGet 依赖项(当前 v.2.1.2)
  • 如果您使用的是Newtonsoft命名空间,例如 Newtonsoft.Json,并且在安装 Microsoft.Data.SqlClient 后找不到此命名空间,则从 Z20055252BAD325BE70C329DCEE6Z 中获取 Newtonsoft.Json 依赖项。

到目前为止,我发现的唯一解决方法是将“Microsoft.EntityFrameworkCore.SqlServer”添加到可执行项目中。 不优雅,但它有效。

我通过使用 System.Data.SqlClient package 解决了这个问题,并将 costom SqlConnection 传递给 dbContext,如下例所示:

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

我遇到了同样的问题,只需将 bin 文件夹(不仅是 .dll 文件)重新上传到生产服务器即可解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM