数据库连接字符串(EF)

来源:互联网 发布:mac ps cs4破解版下载 编辑:程序博客网 时间:2024/05/17 09:31


实体框架允许从配置文件指定大量设置。一般来说,EF 遵循“约定先于配置”的原则。本文讨论的所有设置都有默认行为,您只需负责在默认值不再能够满足需求时更改设置。

所有这些设置都可以使用代码来应用。配置文件选项可用于在部署期间轻松更改这些设置,而无需更新代码。

 

实体框架配置节

从 EF4.1 开始,您可以使用配置文件的 appSettings 一节设置上下文的数据库初始值设定项。在 EF 4.3 中,我们引入了自定义entityFramework 节以处理新设置。实体框架仍能够识别使用旧格式设置的数据库初始值设定项,但我们建议尽量转变为新格式。

当您安装 EntityFramework NuGet 包时,entityFramework 一节已自动添加到项目的配置文件中。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3.   <configSections>
  4.     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  5.     <section name="entityFramework"
  6.        type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  7.   </configSections>
  8. </configuration>

 

连接字符串

本页提供了有关实体框架如何确定要使用的数据库的更多详细信息,包括配置文件中的连接字符串。

连接字符串将存入标准 connectionStrings 元素中,无需 entityFramework 配置节。

基于 Code First 的模型使用一般 ADO.NET 连接字符串。例如:

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

基于 EF 设计器的模型使用特殊 EF 连接字符串。例如:

  1. <connectionStrings>
  2.   <add name="BlogContext" 
  3.         connectionString="metadata=res://*/BloggingModel.csdl|
  4.                                                        res://*/BloggingModel.ssdl|
  5.                                                        res://*/BloggingModel.msl;
  6.                                            provider=System.Data.SqlClient
  7.                                            provider connection string=
  8.                                                &quot;data source=(localdb)\v11.0;
  9.                                                initial catalog=Blogging;
  10.                                                integrated security=True;
  11.                                                multipleactiveresultsets=True;&quot;"
  12.      providerName="System.Data.EntityClient" />
  13. </connectionStrings>

 

Code First 默认连接工厂

该配置节允许您指定 Code First 用来查找要用于上下文的数据库的默认连接工厂。仅在没有为上下文在配置文件中添加连接字符串时,才会用到默认连接工厂。

当您安装 EF NuGet 包时,已注册一个指向 SQL Express 或 LocalDb(取决于您安装的数据库产品)的默认连接工厂。

要设置连接工厂,请在 deafultConnectionFactory 元素中指定程序集限定类型名。
注意:程序集限定名是命名空间限定名,后跟一个逗号,然后是该类型所在的程序集。您还可以选择指定程序集版本、区域性和公钥令牌。

下面是您自己的默认连接工厂的设置示例:

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

以上示例要求该自定义工厂具有无参数构造函数。如果需要,您可以使用 parameters 元素指定构造函数参数。

例如包含在实体框架中的 SqlCeConnectionFactory 要求您向构造函数提供一个提供程序固定名称。提供程序固定名称指示您要使用的 SQL Compact 的版本。以下配置将使上下文在默认情况下使用 SQL Compact 4.0 版。

  1. <entityFramework>
  2.   <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
  3.     <parameters>
  4.       <parameter value="System.Data.SqlServerCe.4.0" />
  5.     </parameters>
  6.   </defaultConnectionFactory>
  7. </entityFramework>

如果您不设置默认连接工厂,则 Code First 将使用指向 .\SQLEXPRESS 的 SqlConnectionFactory。SqlConnectionFactory 也有一个可用来覆盖连接字符串部分的构造函数。如果想使用 SQL Server 实例而不是 .\SQLEXPRESS,可使用此构造函数来设置服务器。

以下配置将使 Code First 对没有显式设置连接字符串的上下文使用 MyDatabaseServer

  1. <entityFramework>
  2.   <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
  3.     <parameters>
  4.       <parameter value="Data Source=MyDatabaseServer; Integrated Security=True; MultipleActiveResultSets=True" />
  5.     </parameters>
  6.   </defaultConnectionFactory>
  7. </entityFramework>

默认情况下,将假定构造函数参数的类型为字符串。您可以使用 type 特性来进行更改。

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

 

数据库初始值设定项

数据库初始值设定项针对每个上下文配置。可以使用 context 元素在配置文件中设置它们。此元素使用程序集限定名来指示要配置的上下文。

默认情况下,Code First 上下文配置为使用 CreateDatabaseIfNotExists 初始值设定项。context 元素有一个可用于禁用数据库初始化的disableDatabaseInitialization 特性。

例如,以下配置禁用在 MyAssembly.dll 中定义的 Blogging.BlogContext 上下文的数据库初始化。

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

可以使用 databaseInitializer 元素设置自定义初始值设定项。

  1. <contexts>
  2.   <context type=" Blogging.BlogContext, MyAssembly">
  3.     <databaseInitializer type="Blogging.MyCustomBlogInitializer, MyAssembly" />
  4.   </context>
  5. </contexts>

构造函数参数使用与默认连接工厂相同的语法。

  1. <contexts>
  2.   <context type=" Blogging.BlogContext, MyAssembly">
  3.     <databaseInitializer type="Blogging.MyCustomBlogInitializer, MyAssembly">
  4.       <parameters>
  5.         <parameter value="MyConstructorParameter" />
  6.       </parameters>
  7.     </databaseInitializer>
  8.   </context>
  9. </contexts>

可以配置在实体框架中包括的泛型数据库初始值设定项之一。type 特性对泛型类型使用 .NET Framework 格式。

例如,如果您要使用 Code First 迁移,则可以使用 MigrateDatabaseToLatestVersion<TContext、 TMigrationsConfiguration> 初始值设定项将数据库配置为自动迁移。

  1. <contexts>
  2.   <context type="Blogging.BlogContext, MyAssembly">
  3.     <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[Blogging.BlogContext, MyAssembly], [Blogging.Migrations.Configuration, MyAssembly]], EntityFramework" />
  4.   </context>
  5. </contexts>
0 0
原创粉丝点击