3

I'm learning to build ASP.NET Core MVC app with Visual Studio with this tutorial. In the "Adding a model" step I created a new separate project (as written in the instructions), but when I rundotnet ef database update, the following error occurs:

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: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)

But I have installed SQL Server and SQLEXPRESS service is running. I also tried to open port, but it didn't help.

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
datafile4
  • 69
  • 2
  • 12

2 Answers2

9

Setup LocalDB

The template's connection string is for localdb and specifically for the mssqllocaldb instance. Make sure that you have both.

appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-MvcMovie-87ad796f-548e-4862-9713-d2976453319e;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

Make sure that you have installed LocalDB. To check, open a command prompt and run sqllocaldb. That will print the LocalDB command line help information. If you don't have LocalDB, then install it.

Make sure that you have an instance named MSSQLLocalDB. To check, run sqllocaldb info. If you do not have that instance, you will need either to create it (sqllocaldb create mssqllocaldb) or to change your connection string to point at an instance that you do have.

Working example on my machine

The dotnet ef database update command runs on my machine. Here is the output of various commands at my prompt.

> sqllocaldb info
MSSQLLocalDB

> dotnet ef migrations add Initial
Project MvcMovie (.NETCoreApp,Version=v1.0) will be compiled...
Compiling MvcMovie for .NETCoreApp,Version=v1.0
Compilation succeeded.
    0 Warning(s)
    0 Error(s)
Time elapsed 00:00:01.7783361

Done. To undo this action, use 'dotnet ef migrations remove'

> dotnet ef database update
Project MvcMovie (.NETCoreApp,Version=v1.0) was previously...
Done.
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
3

I wanted to extend Shaun Luttin's answer. I was having the same problem, but in this case, I didn't come with a fresh Installation of Visual Studio, and it was giving me problems as well.

This was my solution (From Existing installation):

  1. Go to View and Click "SQL Server Object Explorer"
  2. On your server list right click on the Server name and click "Properties"
  3. Look for "Connection String".

Here in my case, it said:

"Source=(localdb)\\ProjectsV13;Initial...

Omitted for brevity

So, replace the Server=(localdb)\\mssql for (localdb)\\ProjectsV13

That should do it.1

enter image description here

Jose A
  • 10,053
  • 11
  • 75
  • 108
  • This is really interesting as for me I assumed this wouldn't work, but yet it did. I've got aliases set up in my MS SQL Configuration to allow me to treat my local instances the same as I have elsewhere and for some reason these aliases seem to be ignored by EF Core migrations. Thanks for this – The Senator Oct 26 '22 at 13:22