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 :

public class FE6CodeConfig : DbConfiguration
{
public FE6CodeConfig()
{
//define configuration here
}
}

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

<entityFramework codeConfigurationType="EF6DBFirstTutorials.FE6CodeConfig, EF6DBFirstTutorials">
</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:

public class FE6CodeConfig : DbConfiguration
{
public FE6CodeConfig()
{
this.SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.SqlConnectionFactory());
}
}

config file:

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

Set Database Provider:

Code-based configuration:

public class FE6CodeConfig : DbConfiguration
{
public FE6CodeConfig()
{
this.SetProviderServices("System.Data.SqlClient",
System.Data.Entity.SqlServer.SqlProviderServices.Instance);
}
}

config file:

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

Set Database Initializers:

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

public class FE6CodeConfig : DbConfiguration
{
public FE6CodeConfig()
{
this.SetDatabaseInitializer<SchoolDBEntities>(new CustomDBInitializer<SchoolDBEntities>());
}
}

config file:

<entityFramework>
<contexts>
<context type="EF6DBFirstTutorials.SchoolDBEntities, EF6DBFirstTutorials" >
<databaseInitializer type="EF6DBFirstTutorials.CustomDBInitializer , EF6DBFirstTutorials">
</databaseInitializer>
</context>
</contexts>
</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. linux 命令详解 sort

    转自:https://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html sort是在Linux里非常常用的一个命令,管排序的,集中精力,五 ...

  2. bzoj 3796 Mushroom追妹纸——后缀数组

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3796 长度一般都是 1e5 ,看这个是 5e4 ,一看就是把两个串接起来做. 自己本来想的是 ...

  3. 如何注册java程序为windows服务

    如何注册java 程序为windows 服务 最近想找个软件来控制电脑的关机时间,在网上找了几个,都是可视化界面的可以设置具体的关机时间的软件.由于我想编写的关机程序是运行在别人机器上,只能让该机器在 ...

  4. 半导体巨头青睐物联网领域 众强联手打造MCU生态系统

    随着万物互联的时代到来,众多半导体巨头纷纷转战物联网领域.早在十年前,意法半导体曾将STM32推向市场,意法半导体对32位MCU在物联网方面的应用在两年前就已展开攻势. 4月25日,历经两届盛况的ST ...

  5. android开源库收集

    1. google valley Android网络通信库 git clone https://android.googlesource.com/platform/frameworks/volley ...

  6. Annotation之一:Java Annotation基本功能介绍

    一.元数据的作用 如果要对于元数据的作用进行分类,目前还没有明确的定义,不过我们可以根据它所起的作用,大致可分为三类: 编写文档:通过代码里标识的元数据生成文档.这是最常见的,也是java 最早提供的 ...

  7. maven学习5 构建MyBatis项目

    2. 修改pom.xml,添加MyBatis依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=& ...

  8. Cocos暂停和重新开始游戏

    创建按钮 cc.MenuItemFont.setFontSize(18); cc.MenuItemFont.setFontName("Arial"); var systemMenu ...

  9. js控制使div自动适应居中

    一直都在想怎么样使弹出的DIV能在任何时候都是居中显示的,刚开始的时候是用CSS样式直接定义好层的位置,但是当页面很长的时候,或是浏览器窗口大小不是固定的时候就不能正确的显示,所以只好用JS来控制DI ...

  10. Win8.1(64bit) Hyper-V 安装Ubuntu 14.04LTS(64 bit)

    为了学习在Linux平台下开发,时隔将近一年多,重新搭建开发环境. 写文档确实很费时间,不过还是很有必要写的,这么一个简单的事情花了接近3个小时才算最终大功告成. 像这种连环嵌套的问题,一旦超过了1个 ...