将Entity Framework Core从3.1.4升级到3.1.8后,SqlConnection.Open失败并引发登录失败,导致用户“ sa”错误登录失败

问题描述

我在ASP.NET Core 3.1 Web API项目中使用Microsoft.EntityFrameworkCore v3.1.4的Nuget软件包来使用TransactionScope读取和写入数据库。为此,我提到了official documentation

这是我的代码

using (var scope = new TransactionScope(TransactionScopeOption.required,new Transactionoptions { IsolationLevel = IsolationLevel.ReadCommitted }))
{
    using (var connection = new sqlConnection(_context.Database.GetDbConnection().ConnectionString))
    {
        connection.open(); // ---> throws error - "Login Failed for user 'sa'."

        // Run raw ADO.NET command in the transaction
        var command = connection.CreateCommand();

        // updating document security in tdoc table
        command.CommandText = query;
        command.ExecuteNonQuery();

        // --------------------- Remaining Code goes here. Removed for brevity--------------

以下是使用的Nuget软件包:

<packagereference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.4">
  <!--<PrivateAssets>all</PrivateAssets>-->
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</packagereference>
<packagereference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" />
<packagereference Include="Microsoft.EntityFrameworkCore.sqlServer" Version="3.1.4" />
<packagereference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.4">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</packagereference>

将上述Nuget软件包从3.1.4更新为3.1.8之后,connection.open()突然引发以下错误

用户“ sa”的登录失败

经过一番谷歌搜索后,我发现我需要在连接字符串中添加Persist Security Info=True;

我以前的连接字符串:

Server=ServerName;Database=DatabaseName;UID=sa;PWD=password;

我将其更改为:

Persist Security Info=True;Data Source=ServerName;Initial Catalog=DatabaseName;User Id=sa;Password=password

但是现在我收到此错误

建立与sql Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称正确,并且已将sql Server配置为允许远程连接。 (提供者:共享内存提供程序,错误:40-无法打开与sql Server的连接)

当我执行context.DbSet.FirstOrDefault()时。

请协助解决此损坏。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...