12

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.

mpen
  • 272,448
  • 266
  • 850
  • 1,236

3 Answers3

10

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

Screenshot from my VM

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.

MBender
  • 5,395
  • 1
  • 42
  • 69
  • Why isn't it "visible out of the box"? Where is it? It *is* stored in a file on my computer, right? Did it get thrown in the temp folder maybe? I will try adding `AttachDBFilename` to my connection string like Darin suggests, but I would really like to know what it's doing. – mpen May 23 '13 at 15:40
  • Since I'm using Windows 7 and full-blown SQL servers for development and LocalDd seems to be installed with VS2012 on W8, I'm having trouble actually checking. Would you mind sharing how you *created a very basic DbContext*? – MBender May 25 '13 at 10:50
  • I uploaded the relevant to [this gist](https://gist.github.com/mnbayazit/89409c1fb880f3704396). Its just a class with 2 fields. – mpen May 25 '13 at 17:43
  • Regarding your last paragraph, I wanted to drop the DB to rebuild all the signatures as I tweaked the function, then I was commenting out the call to the `BuildDb` function and adding in one that actually reads and uses the data. I found that right-clicking the DB in the server-explorer and clicking "close connection" usually gets rid of that error message. Thanks for all your help :-) – mpen May 28 '13 at 23:11
  • Indeed - that's what I meant by `reconsider keeping the connection opened in the server browser`. ;) – MBender May 29 '13 at 08:00
3

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    LocalDB is a service based database, and has nothing to do with SQL Server Compact – ErikEJ May 23 '13 at 06:46
  • Thank you. Tried adding this; it created the DB file which I was able to connect to by choosing "Microsoft SQL Server Database File" in Server Explorer. I can see the table now, but I can't seem to view its contents. – mpen May 24 '13 at 00:55
  • Quite possibly because the database is empty? – MBender May 25 '13 at 11:00
1

In my case was enough to connect to SQL EXPRESS -> Properties -> Files and check Path column in Database files table.enter image description here

Nikita
  • 215
  • 3
  • 13