54

Problem

I don't understand how to make LocalDB show up in the SQL Server Object Explorer. On some VMs, it shows up automatically, on some other VMs, it doesn't. Still, after googling for hours, I don't get it.

Current situation

  1. I have a clean VM
  2. I installed Visual Studio 2015 Community (all default settings)
  3. I let a console application run (Entity Framework 6, code-first, console application) which worked on another VM and created a database automatically which then showed up in the SQL Server Object Explorer; but not this time

The error says:

System.Data.SqlClient.SqlException (0x80131904): 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.) ---> System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified

So, on this VM, no database gets created and nothing shows up in the SQL Server Object Explorer's SQL Server node.

Connecting to LocalDB

What I think to know

  • Visual Studio 2015 Community comes with LocalDB; so everything should just work out of the box, but it doesn't, and I don't know why
  • LocalDB databases are just a pair of files (*.mdf and *.ldf)
  • I've seen the files being created at the default database location at C:\Users\<username>\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\MSSQLLocalDB; but on this VM, there is no such folder
  • The App.config looked every time like this (and it was automatically created that way when I installed Entity Framework 6 the NuGet Package Manager in Visual Studio):

App.config

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Other random comments

  • Earlier, with SQL Server, it was necessary to open up certain ports, but LocalDB runs, as I understand it, as a separate process on demand when started by Visual Studio.
  • I don't know how to debug the SQLException
  • Does LocalDB not come packaged with Visual Studio 2015 Community and do I need to install it separately?
Community
  • 1
  • 1
Lernkurve
  • 20,203
  • 28
  • 86
  • 118
  • 1
    run "sqllocaldb info" in command line and see what databases are there on your VM. – Evk Sep 28 '15 at 20:12
  • Should be installed as far as I remember. Maybe something went wrong during installation. I'd try first to install LocalDb on this machine and see if this fixes the problem. If it does - well it was not installed for one reason or another. – Evk Sep 28 '15 at 21:23
  • 1
    As @Evk mentioned below, LocalDB is now an optional feature to reduce the VS install size. Check the "SQL Server Data Tools" option from the VS 2015 installer and this will ensure LocalDB gets installed. Most other SQL features are still installed by default as their size was very small and they were needed for key scenarios such as Server Explorer connectivity. – Kevin Cunnane Jan 11 '16 at 22:46
  • Related question, how to create localdb progamatically http://stackoverflow.com/questions/25577626/how-does-one-programmatically-create-a-localdb-mdf/36093282#36093282 – Chris Marisic Mar 18 '16 at 20:22

8 Answers8

47

I had the same issue today recently installing VS2015 Community Edition Update 1.

I fixed the problem by just adding the "SQL Server Data Tools" from the VS2015 setup installer... When I ran the installer the first time I selected the "Custom" installation type instead of the "Default". I wanted to see what install options were available but not select anything different than what was already ticked. My assumption was that whatever was already ticked was essentially the default install. But its not.

Gonzalo Lucero
  • 1,717
  • 17
  • 16
  • 10
    I recently downloaded Visual Studio Community Edition, had the same problem, Could not create a local DB. Gonzalo's solution fixed my problem. Only one exception I wanted to add, I had ALREADY installed VS, so I could not add the tools during installation. Instead, go to tools -> Extensions and updates -> Updates -> Product Updates and Install the "Microsoft SQL Server Update...". Then restart Visual studio, rebuild and build your project again. This time it worked for me. I had this 100% exact same scenario on TWO windows 10 computers. Fixed it for me. Thanks gonzalo! – Captainlonate Jan 10 '16 at 05:29
  • hey no worries Captain... yes I think that MS could have done a little better with there naming as "...Tools" to me does not imply it will install a database engine but rather just provide tooling. Thanks – Gonzalo Lucero Jan 22 '16 at 02:33
  • 3
    It appears that (at least) for VS Community 2015.2, "SQL Server Data Tools" is not selected by default during installation. It is an additional 450MB or so, and without it, many updates will not install. Simply re-run the installer and choose modify. – rdtsc Apr 15 '16 at 13:40
  • seems the @Captainlonate 's comment ought to have been an answer that supplements this one – e4c5 Feb 02 '17 at 10:25
26

To check if LocalDb is installed or not:

  • run cmd and type in sqllocaldb i this should give you the installed sqllocaldb instances if found.
  • Run SSMS (SQL Server Management Studio).
  • Try to connect to this instance (localdb)\V11.0 using windows authentication.

If an error is raised Cannot connect to (localdb)\V11.0. change the instance name to (localdb)\MSSQLLocalDB and try again to connect, if you still get the same error.

Follow these steps to install LocalDb:

  • Close SSMS.
  • Close VS (Visual Studio) if it's running.
  • Go to Start Menu and type in search sqlLocalDb.
  • From the results that appears choose sqlLocalDb.msi and click it.
  • SQL setup will start to install LocalDB

after finishing the installation re-run SSMS and try connecting to either of the instances (localdb)\V11.0 or (localdb)\MSSQLLocalDB, one of it should work depending on what Visual Studio version you have.

You can also verify that localdb is installed using Visual Studio by simply creating new sql file and go to the connect icon on the top header of the file which by default lists all the servers you can connect to including localdb if installed.

In addition to the above mentioned ways of finding if localdb is installed, you can also use the MS windows power shell or windows command processor CMD or even NuGet package manager console on your server machine and run these commands sqllocaldb i and sqllocaldb v that will show you the localdb name if it is installed and the MSSQL server version installed and running on your machine.

Ashraf Sada
  • 4,527
  • 2
  • 44
  • 48
  • 2
    This solution will only work if you have SQL Server Management Studio (SSMS) installed. SSMS is not part of the Visual Studio product collection. – WernerVA Jul 24 '16 at 18:48
14

If you are not sure if local db is installed, or not sure which database name you should use to connect to it - try running 'sqllocaldb info' command - it will show you existing localdb databases.

Now, as far as I know, local db should be installed together with Visual Studio 2015. But probably it is not required feature, and if something goes wrong or it cannot be installed for some reason - Visual Studio installation continues still (note that is just my guess). So to be on the safe side don't rely on it will always be installed together with VS.

Evk
  • 98,527
  • 8
  • 141
  • 191
9
  1. Search "sqlLocalDb" from start menu,
  2. Click on the run command,
  3. Go back to VS 2015 tools/connect to database,
  4. select MSSQL server,
  5. enter (localdb)\MSSQLLocalDB as server name

Select your database and ready to go.

Janiya Hicks
  • 91
  • 1
  • 1
4

I tried to install only LocalDB, which was missed in my VS 2015 installation. Followed below URL & selectively download the LocalDB (2012) installer which is only 33mb in size :)

https://www.microsoft.com/en-us/download/details.aspx?id=29062

If you are looking for the SQL Server Data Tool for Visual Studio 2015 Integration, then Please download that from :

https://msdn.microsoft.com/en-us/mt186501

Biki
  • 2,518
  • 8
  • 39
  • 53
1
  1. Search for sqllocaldb or localDB in your windows start menu and right click on open file location
  2. Open command prompt in the file location you found from the search
  3. On your command prompt type sqllocaldb start

  4. Use <add name="defaultconnection" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=tododb;Integrated Security=True" providerName="System.Data.SqlClient" />

Karthikeyan VK
  • 5,310
  • 3
  • 37
  • 50
0
My App.config looks as below:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

I noticed that there is localDB in the path that you mentioned above and has the version v11.0. So I entered (LocalDB\V11.0) in Add Connection dialogue and it worked for me.

shilly
  • 1
0

Another solution (at least for VS2019) if you dont know your localdb ServerName:

Tools => SQL Server => New Query

And in popup just select Local and there you should find Server name(and more) for your localdb