【Entity Framework】EF配置文件设置详解

news/2024/5/19 22:25:56 标签: .netcore, c#, asp.net, linq, microsoft

【Entity Framework】EF配置文件设置详解

在这里插入图片描述

文章目录

  • 【Entity Framework】EF配置文件设置详解
    • 一、概述
    • 二、实体框架配置部分
    • 三、连接字符串
    • 四、EF数据库提供程序
    • 五、EF侦听器
    • 六、将数据库操作记录到文件中
    • 七、Code First默认连接工厂
    • 八、数据库初始值设定项

一、概述

EF实体框架允许在配置文件中指定多个设置。一般来说,EF遵循"约定优于配置"的原则;从EF6开始,引入代码配置,在【Entity Framework】EF配置之代码配置详解 一文详解介绍,代码配置提供了一种代码应用配置的重要的方式。仍然可以从代码应用配置,但需要使用各种 API 来配置不同的区域。 借助配置文件选项可在部署期间轻松更改这些设置,而无需更新代码。

二、实体框架配置部分

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

安装 EntityFramework NuGet 包时,entityFramework 部分会自动添加到项目的配置文件中。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework"
       type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>
</configuration>

三、连接字符串

连接字符串位于标准connectionStrings元素中,不需要entityFramework部分。

  • 基于 Code First 的模型使用常规 ADO.NET 连接字符串
<connectionStrings>
  <add name="BlogContext"  
        providerName="System.Data.SqlClient"  
        connectionString="Server=.\SQLEXPRESS;Database=Blogging;Integrated Security=True;"/>
</connectionStrings>
  • 基于 EF 设计器的模型使用特殊的 EF 连接字符串
<connectionStrings>
  <add name="BlogContext"  
    connectionString=
      "metadata=
        res://*/BloggingModel.csdl|
        res://*/BloggingModel.ssdl|
        res://*/BloggingModel.msl;
      provider=System.Data.SqlClient;
      provider connection string=
        &quot;data source=(localdb)\mssqllocaldb;
        initial catalog=Blogging;
        integrated security=True;
        multipleactiveresultsets=True;&quot;"
     providerName="System.Data.EntityClient" />
</connectionStrings>

四、EF数据库提供程序

在 EF6 之前,数据库提供程序的特定于实体框架的部分必须作为核心 ADO.NET 提供程序的一部分包含在内。 从 EF6 开始,EF 特定部分现在单独管理和注册。

通常无需自行注册提供程序。 此操作通常会在安装时由提供程序完成。

通过在 entityFramework 部分的 providers 子部分下包含 provider 元素来注册提供程序。 提供程序条目有两个必需的属性:

  • invariantName,标识此 EF 提供程序面向的核心 ADO.NET 提供程序
  • type,是 EF 提供程序实现的程序集限定类型名称

在安装实体框架时为注册默认 SQL Server 提供程序而创建的条目,实例如下:

<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

程序集限定名称是命名空间限定名称,后跟逗号,然后是类型所在的程序集。 还可以选择指定程序集版本、区域性和公钥标记。

五、EF侦听器

从EF 6.1开始,可以在配置文件中注册侦听器。通过侦听器可在EF执行某些操作时运行其他逻辑。

通过在 entityFramework 部分的 interceptors 子部分下包含 interceptor 元素来注册侦听器。 例如,以下配置注册了内置的 DatabaseLogger 侦听器,该侦听器将所有数据库操作记录到控制台。

<interceptors>
  <interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework"/>
</interceptors>

六、将数据库操作记录到文件中

如果要将日志记录添加到现有的应用程序以帮助调试问题,则通过配置文件注册侦听器特别有用。 DatabaseLogger 通过提供文件名作为构造函数参数来支持记录到文件。

<interceptors>
  <interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework">
    <parameters>
      <parameter value="D:\EFLog\LogOutput.log"/>
    </parameters>
  </interceptor>
</interceptors>

默认情况下,这将导致每次应用启动时日志文件都被一个新文件覆盖。 要改为附加到日志文件(如果它已经存在),请使用以下操作:

<interceptors>
  <interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework">
    <parameters>
      <parameter value="D:\EFLog\LogOutput.log"/>
      <parameter value="true" type="System.Boolean"/>
    </parameters>
  </interceptor>
</interceptors>

七、Code First默认连接工厂

配置部分允许指定Code First应该使用默认连接工厂来定位要用于上下文的数据库。仅当未将连接字符串添加到上下文的配置文件时,才使用默认连接工厂。

安装 EF NuGet 包时,会注册一个指向 SQL Express 或 LocalDB 的默认连接工厂,具体取决于你安装的连接工厂。

若要设置连接工厂,请在 defaultConnectionFactory 元素中指定程序集限定类型名称。

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

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

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

示例,包含在实体框架中的 SqlCeConnectionFactory 需要你向构造函数提供一个提供程序固定名称。 提供程序固定名称标识要使用的 SQL Compact 版本。 以下配置将导致上下文默认使用 SQL Compact 4.0 版。

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

如果未设置默认连接工厂,Code First 将使用指向 .\SQLEXPRESS 的 SqlConnectionFactory。 SqlConnectionFactory 也有一个构造函数,允许你重写连接字符串的各个部分。 如果要使用 .\SQLEXPRESS 以外的 SQL Server 实例,可以使用此构造函数来设置服务器。

以下配置将导致 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>

八、数据库初始值设定项

数据库初始值设定项按上下文进行配置。 可以使用 context 元素在配置文件中设置它们。 此元素使用程序集限定名称标识要配置的上下文。默认情况下,Code First 上下文配置为使用 CreateDatabaseIfNotExists 初始值设定项。 context 元素上有一个 disableDatabaseInitialization 属性,可用于禁用数据库初始化。

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

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

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

<contexts>
  <context type=" Blogging.BlogContext, MyAssembly">
    <databaseInitializer type="Blogging.MyCustomBlogInitializer, MyAssembly" />
  </context>
</contexts>

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

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

可以配置实体框架中包含的泛型数据库初始值设定项之一。 type 属性将 .NET Framework 格式用于泛型类型。

如果使用的是 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>

http://www.niftyadmin.cn/n/5476358.html

相关文章

vue2使用组件-操作详解

局部注册 1.在src / components /下新建组件 命名使用大驼峰写法&#xff0c;例如LiHeader.vue <template><div class"liHeader"><p>组件</p></div> </template><script> export default {}; </script><style…

关于前端资源文件打包问题

可以使用webpack CopyWebpackPlugin插件 CopyWebpackPlugin是一个用于在构建过程中共复制文件和文件夹的Webpack插件。可以帮助我们将特定的文件或文件夹从源目录复制到构建目录&#xff0c;使得这些文件能够在输出的bundle中被访问到。 使用步骤&#xff1a; 1、安装CopyWeb…

蓝桥杯竞赛经典题型模板

十进制转R进制 #include<iostream> #include<string> #include<algorithm> using namespace std;string tentoR(int x, int r) {if (x 0) return "0"; // 如果x为0&#xff0c;直接返回"0"string s;while (x) {int num x % r;if (num…

大数据在医疗信息化中的应用

大数据在医疗信息化中发挥着至关重要的作用&#xff0c;其应用广泛且深入&#xff0c;主要体现在以下几个方面&#xff1a; 个性化治疗&#xff1a;通过分析患者病历和生理指标等信息&#xff0c;大数据可以为不同患者量身定制治疗方案&#xff0c;提高治疗效果&#xff0c;减…

关于centos8自带的apache2.4开启https后,XP系统的IE8无法显示网页的问题

经检验&#xff0c;是因为系统的apache和openssl版本太高导致的。 禁用系统默认的apache2.4&#xff0c;自己重新源码编译安装一套openssl-1.0.1fapache2.2.23php7.1.2即可。跟update-crypto-policies没有关系&#xff0c;可保持默认的DEFAULT状态。 关于centos8自带的apache2…

(ShardingSphere-JDBC)+(Mybatis)-springboot/快速搭建

阿丹&#xff1a;快速搭建快速使用 希望可以快速完成&#xff0c;可粘贴方式的快速搭建 ShardingSphere-JDBC pom依赖 <!-- ShardingSphere JDBC核心依赖 --><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>shardin…

目标检测——RCNN系列学习(一)

前置知识 包括&#xff1a;非极大值抑制&#xff08;NMS&#xff09;、selective search等 RCNN [1311.2524] Rich feature hierarchies for accurate object detection and semantic segmentation (arxiv.org)https://arxiv.org/abs/1311.2524 1.网络训练 2.推理流程 3.总…

cmake 怎么给CMAKE_TOOLCHAIN_FILE赋值

在使用CMake时&#xff0c;可以通过命令行参数-DCMAKE_TOOLCHAIN_FILE来为CMAKE_TOOLCHAIN_FILE变量赋值。这个参数指定了一个CMake脚本&#xff0c;该脚本包含了特定交叉编译工具链的设置。 例如&#xff0c;如果你有一个名为toolchain.cmake的文件&#xff0c;并且它位于当前…