Where is my database and how do I view it?
This article should answer your question.
The configuration section allows you to specify a default connection factory that Code First should use to locate a database to use for a context. The default connection factory is only used when no connection string has been added to the configuration file for a context.
When you installed the EF NuGet package a default connection factory was registered that points to either SQL Express or LocalDb, depending on which one you have installed.
Judging by your config it appears you are using a connection to LocalDb, which is a minimalist version of SQL used for development.
You can try to use the built-in Server Explorer in Visual Studio to access that database, but, as you wrote, it might not be visible "out of the box". As such, you may need to create a new connection in Server Explorer to see the contents.
EDIT:
I've had to fire up a VMware Windows 8 with VS2012 to actually answer the question "where on the drive is the database".
The LocalDb
creates mdf
and ldf
files in C:\Users\<USER NAME>\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0
As for connecting to it via the server browser, I was able to view the database by entering (LocalDb)\v11.0
as the server address and then selecting the database with a name like the data context name from your application (with namespace).
All of this information I found here.
Please note that in the code you posted here it seems you're rebuilding the database at the start of the application by using Database.SetInitializer(new DropCreateDatabaseAlways<ImageContext>());
. Of course this is good when running the app first time (so the database actually gets created), but subsequent reruns will clear the data and start with a fresh slate. After I had connected to the database using the Server Explorer, I was actually unable to execute this code, as the "database was already in use". You'll likely either need to reconsider keeping the connection opened in the server browser or change that line of code.
You are using LocalDb
. The contents could be stored in a file. You could specify the location by using a connection string:
<connectionStrings>
<add name="ImageContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=database;Integrated Security=SSPI;AttachDBFilename=c:\work\database.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
The name
of the connection string must match with the name of your DbContext
file.
You could use the Server Explorer in Visual Studio to open this db file and explore its contents.