.NET Core can´t connect to SQL DB

In my case the database was accessible via ASP.NET Web API and Windows Forms app. But the .Net Core API wasn't working.

The solution to the problem is to add port number in the connection string.

For e.g. The connection string specified below is example of the .NET Framework app.

data source=SERVER\SQLSERVER2017;initial catalog=DBNAME;persist security info=True;user id=ADMIN;password=ADMIN123;MultipleActiveResultSets=True;App=EntityFramework

I followed this link to set port number to the DB instance.

The port number after comma is the key.

So change connection string to something like this:

Server=SERVER\\SQLSERVER2017,1433;Database=DBNAME;User Id=ADMIN; Password=ADMIN123;MultipleActiveResultSets=True;

We hit a similar (or identical?) issue. .NET Core didn't work with a connection string but .NET Framework worked just fine with that same string. It turned out to be because our connection string was using an SQL Alias rather than the IP hostname of the database server, and .NET Core dropped SQL Alias support because it was too Windowsy/registry-y.

Changing the connection string to use the IP hostname or number resolved the issue.

  • .NET team discussion on issue: https://github.com/dotnet/corefx/issues/2575
  • Related SO issue: Issues with SQL Alias in ConnectionString in appsettings.json for instance.