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.
In the Open dialog box, click the This PC or My Computer shortcut on the left side (or in the Look in box, click My Computer). In the list of drives, right-click the drive that you think might contain the database, and click Search. Enter your search criteria and press ENTER to search for the database.
Expand Databases, right-click the database to view, and then click Properties. In the Database Properties dialog box, select a page to view the corresponding information. For example, select the Files page to view data and log file information.
Database tables and indexes may be stored on disk in one of a number of forms, including ordered/unordered flat files, ISAM, heap files, hash buckets, or B+ trees. Each form has its own particular advantages and disadvantages. The most commonly used forms are B-trees and ISAM.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With