实体框架4代码第一:我的数据库在哪里? [英] Entity Framework 4 Code First: Where is my database?

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

问题描述

首先使用代码创建一个网站,为了我自己的想法,我希望能够在您首次使用数据库并在VS2010中创建SQL Server数据库时,探索其创建的数据库。

Creating a web site using code first, for my own piece of mind i would like to be able to explore the db it creates like you can when you use database first and create the SQL server db within VS2010.

我已经看了一下,唯一相关的信息,我可以发现似乎是使用先前存在的数据库与代码第一,我希望数据库由代码第一,我只是想知道它在哪里,或如何更改它的默认位置,所以我可以看看它。

I have had a look around, and the only related information i can find seems to be using a previously existing db with code first, I want the db to be made by code first and i just want to know where it is, or how to change the default location of it so i can look in it.

我有一个小提琴连接字符串在配置文件中,但不知道我在做什么,所以没有任何成功。它的管理连接字符串工作正常,但我仍然不知道db文件在哪里!

I have had a fiddle with the connection strings in the config file, but don't really know what i am doing so havent had any success there. It worked fine with a custon connection string, but i still don't know where the db file is!

推荐答案

EF代码第一有多种方式来定义数据库的创建位置和方式。实际上它有一些约定(代替具体的配置或代码)。我更喜欢具体(请参阅下面的我的连接字符串示例),但如果您使用惯例,则由Microsoft定义如下:

EF Code First has multiple ways of defining where and how the database gets created. In fact it has a number of Conventions (in lieu of specific configuration or code). I prefer to be specific (see my connection string example below) but if you use the convention it is defined by Microsoft as below:

以下是该页面的代码段: / p>

Here's a snippet from that page:


连接字符串约定

Connection String Convention

实体框架使用默认约定在localhost\SQLEXPRESS实例或LocalDB服务器上创建
数据库,
在派生的
的完全限定类型名称
之后命名数据库上下文(例如CodeFirstModel.SchoolEntities)。

The Entity Framework uses the default conventions to create the database on the localhost\SQLEXPRESS instance or LocalDB server, and names the database after the fully qualified type name of the derived context (for example, CodeFirstModel.SchoolEntities).

Visual Studio 11包括LocalDb数据库服务器,而不是
SQLEXPRESS。在安装过程中,EntityFramework NuGet软件包
检查哪个数据库服务器可用。然后,NuGet软件包将通过设置默认数据库服务器
来更新配置文件,该数据库服务器按照惯例在创建连接时使用Code First。如果
SQLEXPRESS正在运行,它将被使用。如果SQLEXPRESS不可用
,那么LocalDb将被注册为默认值。如果配置文件已经包含默认连接工厂
的设置,则不会对
进行更改。如果连接字符串设置在
代码中,则代码中设置的实例将优先于配置文件中找到的任何

Visual Studio 11 includes LocalDb database server rather than SQLEXPRESS. During installation, the EntityFramework NuGet package checks which database server is available. The NuGet package will then update the configuration file by setting the default database server that Code First uses when creating a connection by convention. If SQLEXPRESS is running, it will be used. If SQLEXPRESS is not available then LocalDb will be registered as the default instead. No changes are made to the configuration file if it already contains a setting for the default connection factory. If the connection string is set in code, then the instance set in code will take precedence over anything found in the config file.

应用程序将在后续运行,除非模型
更改,将使用现有的数据库。如果模型更改,
您不设置初始化程序,则会得到例外:$ db $ b的模型
支持ContextName上下文已更改,因为数据库已创建
。考虑使用代码优先迁移来更新数据库。

When the application is run subsequent times, unless the model changes, the existing database will be used. If the model changes and you do not set the initializer, you will get the exception: "The model backing the 'ContextName’ context has changed since the database was created. Consider using Code First Migrations to update the database."

覆盖数据库名称的代码第一个约定的一种方法是
添加一个App.config或包含与上下文类型相同名称的连接
字符串的Web.config文件。 .config文件
应该添加到包含可执行程序集的项目中。

One way to override the Code First convention for the database name is to add an App.config or Web.config file that contains the connection string with the same name as your context type. The .config file should be added to the project that contains the executable assembly.

注意:使用Code First时,连接字符串应为
a常规数据库连接字符串。当使用Entity
框架设计器时,连接字符串应该是
EntityConnection字符串。

Note: When working with Code First, your connection string should be a regular database connection string. When working with the Entity Framework Designer,, the connection string should be an EntityConnection string.

下面是我的EF Code First连接字符串之一的示例:

Below is an example of one of my EF Code First connection strings:

<add name="NameOfYourDbContextClass" connectionString="Data Source=YOUR-DB-SERVER;Initial Catalog=THE-DB-NAME-YOU-WANT;Persist Security Info=True;Trusted_Connection=true;" providerName="System.Data.SqlClient" />

最后一件事要记住...连接字符串(和一般的配置)由应用程序上下文。换句话说,如果您在另一个项目中拥有数据访问类(基于DbContext),连接字符串仍然需要在web.config(在Web项目中)定义。

One last thing to keep in mind... connection strings (and configuration in general) are defined by the application context. In other words, if you have your Data Access class (based on DbContext) in another project the connection strings still need to be defined in your web.config (in the web project) as an example.

祝你好运本!

这篇关于实体框架4代码第一:我的数据库在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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