我的数据库在哪里,如何查看? [英] Where is my database and how do I view it?

查看:234
本文介绍了我的数据库在哪里,如何查看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚创建了一个全新的应用程序,通过NuGet添加了EntityFramework 5,创建了一个非常基本的DbContext并保存了一些数据。



我的确切位置是数据以及如何查看?它添加的未修改的 App.config

 <?xml版本= 1.0 encoding = utf-8?> 
< configuration>
< configSections>
<!-有关实体框架配置的更多信息,请访问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版本= 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>

通过检查 db 对象,我可以看到我的连接字符串是

  Data Source =(localdb)\\v11.0; Initial Catalog = ImageSignature.ImageContext ; Integrated Security = True; MultipleActiveResultSets = True;应用程序名称= EntityFrameworkMUE 

但是所有这些看起来都像胡言乱语对我来说。我来自MySQL背景。



我的数据库到底在哪里,如何查看?我在VS2012的服务器资源管理器下看不到任何相关内容。

解决方案



请注意您在此处发布的代码中,似乎您正在重新构建数据库通过使用 Database.SetInitializer(new DropCreateDatabaseAlways< ImageContext>()); 。当然,这在第一次运行该应用程序时很好(这样实际上就可以创建数据库了),但是随后的重新运行将清除数据并从头开始。使用服务器资源管理器连接到数据库后,由于数据库已在使用中,实际上我无法执行此代码。您可能需要重新考虑在服务器浏览器中保持连接打开状态,或者更改该行代码。


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.

这篇关于我的数据库在哪里,如何查看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆