Code-based Configuration:

Entity Framework 6 has introduced code based configuration. Now, you can configure Entity Framework related settings using the code which had been previously configured in the <entityframework> section of the app.config. However, app.config takes precedence over code-based configuration. In other words, if a configuration option is set in both the code and in the config file, then the setting in the config file is used.

Let's see how to implement code-based configuration using Entity Framework 6.

First of all, you need to create a new class that derives the DbConfiguration (System.Data.Entity.DbConfiguration) class :

  1. public class FE6CodeConfig : DbConfiguration
  2. {
  3. public FE6CodeConfig()
  4. {
  5. //define configuration here
  6. }
  7. }

Now, you can set codeConfigurationType in the config file as shown below:

  1. <entityFramework codeConfigurationType="EF6DBFirstTutorials.FE6CodeConfig, EF6DBFirstTutorials">
  2. </entityFramework>

Or you can use the DbConfigurationType attribute on the context class to set the code-based configuration class:

Note: EF does not support having multiple configuration classes used in the same AppDomain. If you use this attribute, to set different configuration classes for two contexts, then an exception will be thrown.

Now, you can use different methods of DbConfiguration using this in the constructor as shown below:

Let's see how to do different settings using code-based configuration as well as the config file.

Set default connection factory:

Code-based configuration:

  1. public class FE6CodeConfig : DbConfiguration
  2. {
  3. public FE6CodeConfig()
  4. {
  5. this.SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.SqlConnectionFactory());
  6. }
  7. }

config file:

  1. <entityFramework>
  2. <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  3. </entityFramework>

Set Database Provider:

Code-based configuration:

  1. public class FE6CodeConfig : DbConfiguration
  2. {
  3. public FE6CodeConfig()
  4. {
  5. this.SetProviderServices("System.Data.SqlClient",
  6. System.Data.Entity.SqlServer.SqlProviderServices.Instance);
  7. }
  8. }

config file:

  1. <entityFramework>
  2. <providers>
  3. <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  4. </providers>
  5. </entityFramework>

Set Database Initializers:

You can set database initializers (for Code-First only) using code-based configuration as shown below:

  1. public class FE6CodeConfig : DbConfiguration
  2. {
  3. public FE6CodeConfig()
  4. {
  5. this.SetDatabaseInitializer<SchoolDBEntities>(new CustomDBInitializer<SchoolDBEntities>());
  6. }
  7. }

config file:

  1. <entityFramework>
  2. <contexts>
  3. <context type="EF6DBFirstTutorials.SchoolDBEntities, EF6DBFirstTutorials" >
  4. <databaseInitializer type="EF6DBFirstTutorials.CustomDBInitializer , EF6DBFirstTutorials">
  5. </databaseInitializer>
  6. </context>
  7. </contexts>
  8. </entityFramework>

Download sample project for Code-based configuration demo.

Entity Framework 6.0 Tutorials(3):Code-based Configuration的更多相关文章

  1. Entity Framework 6.0 Tutorials(1):Introduction

    以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.a ...

  2. Entity Framework 6.0 Tutorials(4):Database Command Logging

    Database Command Logging: In this section, you will learn how to log commands & queries sent to ...

  3. Entity Framework 6.0 Tutorials(11):Download Sample Project

    Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...

  4. Entity Framework 6.0 Tutorials(10):Index Attribute

    Index Attribute: Entity Framework 6 provides Index attribute to create Index on a particular column ...

  5. Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping

    Code First - Insert, Update, Delete Stored Procedure Mapping: Entity Framework 6 Code-First provides ...

  6. Entity Framework 6.0 Tutorials(6):Transaction support

    Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...

  7. Entity Framework 6.0 Tutorials(2):Async query and Save

    Async query and Save: You can take advantage of asynchronous execution of .Net 4.5 with Entity Frame ...

  8. Entity Framework 6.0 Tutorials(8):Custom Code-First Conventions

    Custom Code-First Conventions: Code-First has a set of default behaviors for the models that are ref ...

  9. Entity Framework 6.0 Tutorials(7):DbSet.AddRange & DbSet.RemoveRange

    DbSet.AddRange & DbSet.RemoveRange: DbSet in EF 6 has introduced new methods AddRange & Remo ...

随机推荐

  1. HTTP 和 SOAP

    http:是一个客户端和服务器端请求和应答的标准(TCP).http协议其目的是为了提供一种发布和接收htttp页面的方法 一http协议的客户端与服务器的交互:由HTTP客户端发起一个请求,建立一个 ...

  2. (转)RadioButton左侧显示文字,右侧显示按钮时文字不靠边的问题解决

    作者:  发布日期:2014-02-13 21:00:45 我来说两句(0) 0 Tag标签:RadioButton  左侧  显示   项目中有一个这样的需求: 下面三行点击某行即选中,颜色变深.自 ...

  3. ubuntu上安装Adminer

    Apache 安装 $ sudo apt-get install apache2 php 安装 $ sudo apt-get install php7.0 $ sudo apt-get install ...

  4. sublime text3 自动编译php 适合用于简单的php文件执行

    1.将php路径放入环境变量中 2. 点击 sublime_text的“工具”->"编译系统"->"编译新系统" { "cmd" ...

  5. SVN安装和使用总结

    SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...

  6. 1.3Broker

    Celery需要一种解决消息的发送和接受的方式,我们把这种用来存储消息的的中间装置叫做message broker, 也可叫做消息中间人. 作为中间人,我们有几种方案可选择: 1.RabbitMQ R ...

  7. leetcode594

    public class Solution { public int FindLHS(int[] nums) { Dictionary<int, int> dic = new Dictio ...

  8. Java微信公众平台开发(十四)【番外篇】--微信web开发者工具使用

    转自:http://www.cuiyongzhi.com/post/58.html 为帮助开发者更方便.更安全地开发和调试基于微信的网页,微信推出了 web 开发者工具.它是一个桌面应用,通过模拟微信 ...

  9. Java微信公众平台开发【番外篇】(七)--公众平台测试帐号的申请

    转自:http://www.cuiyongzhi.com/post/45.html 前面几篇一直都在写一些比较基础接口的使用,在这个过程中一直使用的都是我个人微博认证的一个个人账号,原本准备这篇是写[ ...

  10. 为cscope查找列表增添色彩

    我在使用cscope的时候,偏好于不用quickfix窗口来显示查找列表,而是选择类似ctag的列表.但这会带来一个比较麻烦的问题,就是窗口列表一片白色,看起来非常难受: 特别是当搜索结果特别多的时候 ...