TransWikia.com

SqlConnection.Open fails and throw Login failed for user 'sa' error suddenly after upgrading Entity Framework Core from 3.1.4 to 3.1.8

Stack Overflow Asked by fingers10 on December 10, 2020

I was using Microsoft.EntityFrameworkCore v3.1.4 Nuget Package in my ASP.NET Core 3.1 Web API project to read and write to database using TransactionScope. I referred the official documentation for doing that.

Here is my code:

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--------------

Here are the Nuget packages used:

<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>

After updating above Nuget packages from 3.1.4 to 3.1.8, connection.Open() suddenly throws the following error:

Login failed for user ‘sa’

After some googling I found that I need to add Persist Security Info=True; in my connection string.

My previous connection string:

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

I changed this to:

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

But now I get this error:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Shared Memory Provider, error: 40 – Could not open a connection to SQL Server)

when I execute context.DbSet.FirstOrDefault().

Please assist on how to fix this breakage.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP