While developing an ASP.NET Core Web API using Entity Framework Core and SQL Server, one of the most common migration errors developers encounter is:
A network-related or instance-specific error occurred while establishing a connection to SQL Server.
(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Error Number:2, State:0, Class:20
This error usually appears while executing:
Update-Databaseor
dotnet ef database updateAlthough the message appears to indicate a network issue, the actual root cause is often related to SQL Server configuration, an incorrect connection string, startup project selection, or Entity Framework Core design-time configuration.
This article explains the complete troubleshooting process used by professional .NET developers to diagnose and resolve this issue.
Understanding the Error
When Entity Framework Core executes a migration, it attempts to establish a connection with SQL Server.
If SQL Server cannot be reached using the configured connection string, the following exception is thrown:
(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)This does not necessarily mean SQL Server is unavailable. It simply means the application could not establish a connection.
Step 1 – Verify SQL Server Service
First, verify that SQL Server is installed and running.
Open Command Prompt and execute:
sc query MSSQL$SQLEXPRESSExpected Output:
SERVICE_NAME: MSSQL$SQLEXPRESS
TYPE : 10 WIN32_OWN_PROCESS
STATE : 4 RUNNING
(STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0If the service is stopped:
Press Win + R
Type:
services.mscLocate SQL Server (SQLEXPRESS)
Click Start
Step 2 – Verify SQL Server Connectivity
Before debugging your application, verify that SQL Server itself is accessible.
List Installed SQL Server Instances
sqlcmd -LExample Output:
Servers:
DEV-SERVER\SQLEXPRESSIf no server is listed, SQL Server Browser may not be running or your SQL Server instance may not be discoverable.
Connect to SQL Server
sqlcmd -S .\SQLEXPRESS -Eor
sqlcmd -S DEV-SERVER\SQLEXPRESS -ESuccessful connection:
1>This confirms:
SQL Server is installed
SQL Server service is running
Windows Authentication is working
SQL Server accepts connections
Step 3 – Verify SQL Server Information
After connecting successfully, execute the following SQL commands in cmd .
Check Connected Server
SELECT @@SERVERNAME;
2> GO click enter
Output
--------------------------------------------------------------------------------------------------------------------------------
DEV-SERVER\SQLEXPRESS Check SQL Server Version
SeLECT @@VERSION;
2> GO click enter
Output
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Microsoft SQL Server 2022 (RTM) - 16.0.1000.6 (X64)
Oct 8 2022 05:58:25
Copyright (C) 2022 Microsoft Corporation
Express Edition (64-bit) on Windows 10 Pro 10.0 <X64> (Build 26200: ) (Hypervisor)
(1 rows affected)Verify Current Database
SELECT DB_NAME();
GOExpected:
masterList All Databases
SELECT name
2> FROM sys.databases
3> ORDER BY name;
4> GO click enter
Output
name
--------------------------------------------------------------------------------------------------------------------------------
1000SQLDB
model
msdb
SqlInDepth
SQLJourney
SQLPracticeQuery
tempdb
TestData Verify Current Login
SELECT SYSTEM_USER;
2> GO click enter
Output
--------------------------------------------------------------------------------------------------------------------------------
DEV-SERVER\HP
(1 rows affected)Verify Authentication Mode
SELECT SERVERPROPERTY('IsIntegratedSecurityOnly');
GOOutput:
| Value | Meaning |
|---|---|
| 1 | Windows Authentication Only |
| 0 | Mixed Authentication |
Exit sqlcmd
EXIT
Join the conversation! Your thoughts help the community grow.