Entity Framework Code First - Where is my database
So you have started up a code first project using Entity Framework - You're happily coding along but at some point you probably want to know where your database is.
Since all of the wiring up of the database happens automatically when you're running the update-database command you might not be aware of where it is.
If you're reading this, chances are that you're running SQL Server Express LocalDB because that is what Entity Framework chooses for you out of the box. This article focuses on locating instances and files for LocalDB - Let's get on with it...
Connect to LocalDB through Server Explorer
You can connect to your LocalDB through the Server Explore in Visual Studio. Depending on whether you're using SQL Server 2012 or 2014/2016 the instance name differs.
For all versions go into Server Explorer and click "Connect to Database"
SQL Server 2012 Express LocalDB
In Server name enter - (localdb)\v11.0
SQL Server 2014/2016 Express LocalDB
In Server name enter - (localdb)\MSSQLLocalDB
You can also connect using SSMS in the same manner.
Getting code first connectionstring
A small trick that might also come in handy is getting the connectionstring your DBContext is using
using (var context = new MyDbContext())
{
Console.WriteLine(context.Database.Connection.ConnectionString);
}
Locating LocalDB database file
Not much to this if you know where to look.
By default LocalDB files are located in
C:\Users\<user>
Or just type %USERPROFILE% in File Explorer
Hopefully you have now found your database - Happy coding
You might also be interested in these articles...