问题描述
我在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()
突然引发以下错误:
经过一番谷歌搜索后,我发现我需要在连接字符串中添加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 (将#修改为@)