Config File Settings Of EF——实体框架的配置文件设置
我亦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>
Config File Settings Of EF——实体框架的配置文件设置的更多相关文章
- EF实体框架之CodeFirst一
对于SQL Server.MySql.Oracle等这些传统的数据库,基本都是关系型数据库,都是体现实体与实体之间的联系,在以前开发时,可能先根据需求设计数据库,然后在写Model和业务逻辑,对于Mo ...
- EF实体框架处理实体之间关联关系与EF延迟机制(下)
在数据库中,表与表之间可能存在多种联系,比如,一对多,多对多的关系.当我们使用逻辑外键在数据库建立两张表之间的关系的时候,我们使用EF实体框架 必然也会将这种关系映射到我们的实体关系中来.所以,在我们 ...
- BIM工程信息管理系统-EF实体框架数据操作基类
EF实体框架数据操作基类主要是规范增.改.查.分页.Lambda表达式条件处理,以及异步操作等特性,这样能够尽可能的符合基类这个特殊类的定义,实现功能接口的最大化重用和统一. 1.程序代码 /// & ...
- EF实体框架之CodeFirst四
在EF实体框架之CodeFirst二中也提到数据库里面一般包括表.列.约束.主外键.级联操作.实体关系(E-R图).存储过程.视图.锁.事务.数据库结构更新等.前面几篇博客把表.存储过程.视图这些算是 ...
- C#.Net EF实体框架入门视频教程
当前位置: 主页 > 编程开发 > C_VC视频教程 > C#.Net EF实体框架入门视频教程 > kingstone金士顿手机内存卡16G仅65元 1.EF实体框架之增加查 ...
- 【MVC 1】MVC+EF实体框架—原理解析
导读:在之前,我们学过了三层框架,即:UI.BLL.DAL.我们将页面显示.逻辑处理和数据访问进行分层,避免了一层.两层的混乱.而后,我们又在经典三层的基础上,应用设计模式:外观.抽象工厂+反射,使得 ...
- 【EF 1】EF实体框架 原理+实例
一.知识回顾 到目前为止,自己学到的链接数据库操作已经经历了几个阶段,分别是:学生信息管理和(第一次)机房收费时的直接连接数据库操作表格,然后是机房个人重构中应用的操作实体,在其中还利用了一个很重要的 ...
- EF实体框架数据操作基类(转)
//----------------------------------------------------------------// Copyright (C) 2013 河南禄恒软件科技有限公司 ...
- EF实体框架数据操作接口(转)
//----------------------------------------------------------------// Copyright (C) 2013 河南禄恒软件科技有限公司 ...
随机推荐
- 极限挑战—C#+ODP 100万条数据导入Oracle数据库仅用不到1秒
链接地址:http://www.cnblogs.com/armyfai/p/4646213.html 要:在这里我们将看到的是C#中利用ODP实现在Oracle数据库中瞬间导入百万级数据,这对快速批量 ...
- 先有Delphi内存对象,后有句柄(如果需要的话),最后再显示
在设计期放上一个Panel1和Button1,然后设置Panel1.Visible:=False 这时候执行: procedure TForm1.Button4Click(Sender: TObjec ...
- MSSQL - 逻辑主键、业务主键和复合主键
转载自:http://blog.csdn.net/sunrise918/article/details/5575054 这几天对逻辑主键.业务主键和复合主键进行了一些思考,也在网上搜索了一下相关的讨论 ...
- QSettings操作配置文件
用Qt写界面时,难免会进行本地信息的保存,可以使用轻量级数据库sqlite,也可以使用QSettings读写配置文件. 如何来进行读写呢?如下,使用QSettings写一个通用的读写方法: ...
- Ubuntu下使用sshfs挂载远程目录到本地(和Windows挂载盘一样)
访问局域网中其他Ubuntu机器,在不同机器间跳来跳去,很是麻烦,如果能够把远程目录映射到本地无疑会大大方面使用,就像Windows下的网络映射盘一样.在Linux的世界无疑也会有这种机制和方式,最近 ...
- Oracle 的一张表没有主键,如何映射Hibernate
我的一个Oracle表,没有任何主键,然后生成的时候就将所有的字段都作为联合主键,如果所有的字段都做联合主键的话,这样只要一个字段为null,查询的话这条记录就不能查询到. 然后我想到Oracle数据 ...
- 10个优秀的 HTML5 & CSS3 下拉菜单制作教程
下拉菜单是一个非经常见的效果.在站点设计中被广泛使用.通过使用下拉菜单.设计者不仅能够在站点设计中营造出色的视觉吸引力,但也能够为站点提供了一个有效的导航方案.使用 HTML5 和 CSS3 能够更e ...
- jquery prop和attr的区别
jquery1.6中新加了一个方法prop(),一直没用过它,官方解释只有一句话:获取在匹配的元素集中的第一个元素的属性值. 大家都知道有的浏览器只要写disabled,checked就可以了,而有的 ...
- HashSet的排序
import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util ...
- 其他主机连接本地主机Tomcat会出现的防火墙问题
当我在A机上开启Tomcat后,B机上打开浏览器不能访问到Tomcat的服务器,这是由于Windows防火墙的原因 可以由以下两种做法: 关闭Windows防火墙: 如果不想关闭Windows防火墙, ...