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. LG4779 【模板】单源最短路径(标准版)

    题意 给定一个 \(N\) 个点,\(M\) 条有向边的带非负权图,请你计算从 \(S\) 出发,到每个点的距离. 数据保证你能从 \(S\) 出发到任意点. \(1≤N≤100000\): \(1≤ ...

  2. rtrim,dirname,魔术常量用法

    $str = "Hello World!!!"; echo $str . "<br>"; echo rtrim($str,"!d" ...

  3. spring新心得

    一直觉得spring是最厉害的框架,说说最近从依葫芦画瓢到现在慢慢摸索他的思想的过程 以前什么都不懂,在xml上抄网上的东西,到大概知道是什么运作的 三种配装方式 1,<spring实战> ...

  4. 关于app集成支付宝应用内支付的问题总结

    pem文件生成,将合作伙伴密钥复制到notepad++中,每45个字符回车,去除空格,头尾加上标题,文件需保存为无BOM的UTF8格式,就OK.  可以每行64个字符,共216个字符.   近来处理了 ...

  5. 关于android方向传感器的使用

    Android2.2以后 orientation sensors 就被deprecated了 官方建议用acceleration and magnetic sensor 来算 关于这个问题,CSDN上 ...

  6. Source Insight中文字体设置

    Source Insight是一个面向项目开发的程序编辑器和代码阅读工具,它拥有内置的对C/C++, C#和Java等程序的分析,分析你的源代 码并在你工作的同时动态维护它自己的符号数据库,并自动为你 ...

  7. curl -I 说明

    curl -I 查看header头信息

  8. Spring security 浅谈用户验证机制

    step1:首先ApplicationUserDetailsService需要实现UserDetailsService接口(在 org.springframework.security.core.us ...

  9. java多线程的练习------------。加深

    总结:线程的理解不够.还不够 package com.aa; public class MyThread implements Runnable {// 我们可以继承一个Thread.但是我们可以实现 ...

  10. java代码=--数组复制

    总结:arraycopy注意数组定义的长度.不足会补0 package clientFrame; //数组的复制arraycopy() public class Xiang { public stat ...