I've just created a brand new application, added EntityFramework 5 via NuGet, created a very basic DbContext and saved some data to it.
Where exactly is my data and how do I view it? The unmodified App.config
it added is
<?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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
And by inspecting the db
object I can see that my connection string is
Data Source=(localdb)\\v11.0;Initial Catalog=ImageSignature.ImageContext;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFrameworkMUE
But all of that looks like jibberish to me. I come from a MySQL background.
Where exactly is my database, and how can I view it? I don't see anything relevant under "Server Explorer" in VS2012.
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.