My code against LocalDB works but I can't see the database anywhere
-
21-12-2019 - |
Question
I have a simple website that uses EF Code First and LocalDB. This is in my web.config:
<connectionStrings>
<add name="MyContext" providerName="System.Data.SqlClient" connectionString="Server=(localdb)\v11.0;Integrated Security=true;"/>
</connectionStrings>
My website works fine, PowerShell commands like Update-Database
and Add-Migration
work fine, however, I cannot see the database anywhere inside Visual Studio. Not in SQL Server Object Explorer, not in Server Explorer (when I try to connect to "MyContext" database on the (localdb)\v11
server, the dialog tells me that there is no such db there).
I am confused. Where is my database?
Solution
There is one property define - Initial Catalog
Data Source=(localdb)\v11.0;Initial Catalog=YourDbName;Integrated Security=True;
OTHER TIPS
You need to specify the name of the database within your connection string, like this:
Server=(localdb)\v11.0;Database=YourDbName;Integrated Security=SSPI;
or, in the more typical format (since it's the format generated by SqlConnectionStringBuilder
):
Data Source=(localdb)\v11.0;Initial Catalog=YourDbName;Integrated Security=True;
It's worth noting for seasoned developers that the use of "SSPI" is encouraged :
When
false
, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.
Recognized values aretrue
,false
,yes
,no
, andsspi
(strongly recommended), which is equivalent totrue
.
And also that the Connection String docs on MSDN are quite exhaustive if you're wondering what keys are valid for all providers, and also which keys are valid for the SqlClient provider.