I'm trying to setup a database for my ASP.NET Core Web Application. Unfortunately each time I try to run dotnet ef Database Update
, I get this error:
Error Number:-1983577829,State:0,Class:20
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: SNI_PN11, error: 50 - Local Database Runtime err
or occurred. Specified LocalDB instance name is invalid.)
I've tried everything I can think of, and have reinstalled multiple times. I've installed Visual Studio 2019 with LocalDB, 2022 with LocalDB, LocalDB by itself, SQLExpress with LocalDB, and it all has not worked. I've even reset my PC, which unfortunately has still not changed anything.
Here is my connection string:
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=PROJECT_NAME_HERE;Trusted_Connection=True;MultipleActiveResultSets=true"
And I am utilizing the connection string with this:
services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
});
I should mention that this connection string has worked in the past. Any help would be appreciated! Thanks :D
CodePudding user response:
After more and more Googling and researching, I found someone on Reddit that is having a very similar issue, with the same error message through the Event Viewer. The common ground between us is Windows 11, we've both exhausted all of our options.
I thought it might have been because I was on Windows 11, but I thought the chances of that were very minor. I guess it is though. My apologies!
Tl;dr: The assumption is that Windows 11 is not working properly with the SQL Installations.
CodePudding user response:
You probably need to re-create your LocalDB instance. I once had a similar issue when I tried to uninstall and reinstall localdb and sql server express.
You could try running the following command in PowerShell
SqlLocalDB.exe create "MsSQLLocalDB" 12.0 -s
That should create an instance of localdb called "MSSQLLocalDB". You can then use the connection string "Server=(localdb)\\MSSQLLocalDB;Database=PROJECT_NAME_HERE;Trusted_Connection=True;MultipleActiveResultSets=true"
See This MSDN page for more information regarding LocalDB Instances. You could also read more about how localdb works on this MSDN page