Config File Settings Of EF——实体框架的配置文件设置

来源:互联网 发布:基金模拟交易软件 编辑:程序博客网 时间:2024/06/05 22:35

我亦MSDN
原文地址

http://msdn.microsoft.com/en-us/data/jj556606


Entity Framework allows a number of settings to be specified from the configuration file. In general EF follows a ‘convention over configuration’ principle. All the settings

discussed in this post have a default behavior, you only need to worry about changing the setting when the default no longer satisfies your requirements.

 

Entity Framework允许在配置文件中定义若干项设置,大体上EF遵从“公约对配置”的原则。本文中所有这些设置都有一个默认的行为相对应,因此在修改设置的时候,你要考虑好这些默认行为也已经改变,可能不再符合你的需求。

 

All of these settings can also be applied using code. The configuration file option allows these settings to be easily changed during deployment without updating your code.

当然所有这些 配置也还是能够通过代码的方式去实现。但是使用配置文件的方式去设置能够帮助你在开发中轻松的改变配置选项而不需要更新代码。

 

The Entity Framework Configuration Section

Entity Frameworkp配置项

 

Starting with EF4.1 you could set the database initializer for a context using theappSettings section of the configuration file. In EF 4.3 we introduced the custom entityFramework section to handle the new settings. Entity Framework will still recognize database initializers set using the old format, but we recommend moving to the new

在使用EF4.1中设置数据库初始设定,创建上下文是使用配置文件里appSettings节点完成的。在EF4.3中我们推荐通过自定义entityFramework节点去处理这些新设置。EF仍然能够识别以老的方式设置的数据库初始化,但是我们推荐您在可能的情况下尽量使用新的方式。

format where possible.

The entityFramework section was automatically added to the configuration file of your project when you installed the EntityFramework NuGet package.

如果您安装了EntityFramework NuGet package的话,下面的entityFramework相关配置会被自动添加到项目的配置文件中去。

<?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=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />  </configSections></configuration>


Connection Strings

连接字符串

This page provides more details on how Entity Framework determines the database to be used, including connection strings in the configuration file.

这一页提供更多关于Entity Framework是怎样决定使用数据库的一些详细信息,还包括关于配置文件中的连接字符串的介绍,可以参阅。

Connection strings go in the standard connectionStrings element and do not require theentityFramework section.

连接字符串在标准connectionStrings节点中定义,并不需要entityFramework节点。

Code First based models use normal ADO.NET connection strings. For example:

基于代码优先的models使用正常的ADO.NET连接字符串。比如下面的:

<connectionStrings>  <add name="BlogContext"         providerName="System.Data.SqlClient"         connectionString="Server=.\SQLEXPRESS;Database=Blogging;Integrated Security=True;"/></connectionStrings>


 

EF Designer based models use special EF connection strings. For example:

基于EF Designer(实体框架设计器)创建的models则使用专门的实体框架连接字符串,比如这样的:

 

<connectionStrings>  <add name="BlogContext"         connectionString="metadata=res://*/BloggingModel.csdl|                                                       res://*/BloggingModel.ssdl|                                                       res://*/BloggingModel.msl;                                           provider=System.Data.SqlClient                                           provider connection string=                                               "data source=(localdb)\v11.0;                                               initial catalog=Blogging;                                               integrated security=True;                                               multipleactiveresultsets=True;""     providerName="System.Data.EntityClient" /></connectionStrings>

 Code First Default Connection Factory

代码优先的默认连接工厂

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.

通过配置项你可以指定一个默认的连接工厂,用来使Code First找到数据库并创建访问上下文。只有在配置文件中没有连接字符串的时候,才会使用默认的连接工厂创建访问上下文。

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.

您安装EF NuGet package的时候,一个默认的连接工厂就已经注册到SQL Express或者LocalDb了,至于是哪一个,要看您安装的是哪一个。(NuGet是 Visual Studio管理类库的一个扩展)

To set a connection factory, you specify the assembly qualified type name in thedeafultConnectionFactory element.

要设置一个连接工厂,您需要在deafultConnectionFactory元素指定程序集限定类型名称
Note:An assembly qualified name is the namespace qualified name, followed by a comma, then the assembly that the type resides in. You can optionally also specify the assembly version, culture and public key token.

注意:一个程序集限定名称的组成是先有一个命名空间名称,后面跟一个逗号,然后是程序集的名称。你还能指定程序集的版本,区域性和公钥标记。

Here is an example of setting your own default connection factory:

这有一个帮您设置默认连接工厂的示例:

<entityFramework>  <defaultConnectionFactory type="MyNamespace.MyCustomFactory, MyAssembly"/></entityFramework>

The above example requires the custom factory to have a parameterless constructor. If needed, you can specify constructor parameters using theparameters element.

上面的例子只要求自定义工厂有一个不需要参数的访问接口(构造函数)。如果有需要,你还可以通过指定parameters元素指定访问接口的参数。

For example, the SqlCeConnectionFactory, that is included in Entity Framework, requires you to supply a provider invariant name to the constructor. The provider invariant name identifies the version of SQL Compact you want to use. The following configuration will cause contexts to use SQL Compact version 4.0 by default.

例如:在实体框架中有一个SqlCeConnectionFactory工厂,需要你提供访问接口的固定名称。该名称标示您需要的SQL Compact提供程序版本,下面的配置会使上下文默认使用SQL Compact version 4.0的提供程序。

<entityFramework>  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">    <parameters>      <parameter value="System.Data.SqlServerCe.4.0" />    </parameters>  </defaultConnectionFactory></entityFramework>

 

If you don’t set a default connection factory, Code First uses the SqlConnectionFactory, pointing to .\SQLEXPRESS. SqlConnectionFactory also has a constructor that allows you to override parts of the connection string. If you want to use a SQL Server instance other than .\SQLEXPRESS you can use this constructor to set the server.

如果不设置默认连接工厂,Code First会使用SqlConnectionFactory工厂,该工厂指向.\SQLEXPRESS。SqlConnectionFactory还有一个构造函数允许您重写连接字符串的部分内容。假如您想要使用另一个SQL Server 实例而不是.\SQLEXPRESS的话,您可以使用这个构造函数去设置数据库的服务器。

The following configuration will cause Code First to use MyDatabaseServer for contexts that don’t have an explicit connection string set.

下面的配置会导致Code First对没有显式设置连接字符串的上下文使用MyDatabaseServer(数据库服务实例)。

 

<entityFramework>  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">    <parameters>      <parameter value="Data Source=MyDatabaseServer; Integrated Security=True; MultipleActiveResultSets=True" />    </parameters>  </defaultConnectionFactory></entityFramework>


By default, it’s assumed that constructor arguments are of type string. You can use the type attribute to change this.

默认的情况下,构造函数的参数类型被假定为字符串类型,你可以使用type属性更改这个设置。

 

<parameter value="2" type="System.Int32"/>


Database Initializers

数据库初始化

Database initializers are configured on a per-context basis. They can be set in the configuration file using thecontext element. This element uses the assembly qualified name to identify the context being configured.

数据库的初始值设定项建立在每一个具体上下文的基础上。这可以通过配置文件中的contex元素进行设置。该元素使用程序集的 限定名称标识正在配置的上下文。

By default, Code First contexts are configured to use the CreateDatabaseIfNotExists initializer. There is adisableDatabaseInitialization attribute on thecontext element that can be used to disable database initialization.
 默认的,Code First上下文被配置成使用CreateDatabaseIfNotExists的初始值,在context元素中有一个disableDatabaseInitialization属性可以用来设置禁止数据库初始化操作。

<contexts>  <context type=" Blogging.BlogContext, MyAssembly" disableDatabaseInitialization="true" /></contexts>



 

Constructor parameters use the same syntax as default connection factories.

(数据库初始设定的)构造参数使用和连接工厂一样的语法。

<contexts>  <context type=" Blogging.BlogContext, MyAssembly">    <databaseInitializer type="Blogging.MyCustomBlogInitializer, MyAssembly">      <parameters>        <parameter value="MyConstructorParameter" />      </parameters>    </databaseInitializer>  </context></contexts>


 

You can configure one of the generic database initializers that are included in Entity Framework. Thetype attribute uses the .NET Framework format for generic types.

你可以配置一个包含在实体框架中的通用数据库初始值设定项。

For example, if you are using Code First Migrations, you can configure the database to be migrated automatically using the MigrateDatabaseToLatestVersion<TContext, TMigrationsConfiguration> initializer.

例如:如果你在使用Code First做迁移,你可以通过MigrateDatabaseToLatestVersion<TContext, TMigrationsConfiguration>初始值设定项配置数据库自动迁移

<contexts>  <context type="Blogging.BlogContext, MyAssembly">    <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[Blogging.BlogContext, MyAssembly], [Blogging.Migrations.Configuration, MyAssembly]], EntityFramework" />  </context></contexts>