Entity Framework 6.0 Tutorials(3):Code-based Configuration
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的更多相关文章
- Entity Framework 6.0 Tutorials(1):Introduction
以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.a ...
- 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 ...
- Entity Framework 6.0 Tutorials(11):Download Sample Project
Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...
- Entity Framework 6.0 Tutorials(10):Index Attribute
Index Attribute: Entity Framework 6 provides Index attribute to create Index on a particular column ...
- Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping
Code First - Insert, Update, Delete Stored Procedure Mapping: Entity Framework 6 Code-First provides ...
- Entity Framework 6.0 Tutorials(6):Transaction support
Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...
- 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 ...
- 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 ...
- Entity Framework 6.0 Tutorials(7):DbSet.AddRange & DbSet.RemoveRange
DbSet.AddRange & DbSet.RemoveRange: DbSet in EF 6 has introduced new methods AddRange & Remo ...
随机推荐
- BZOJ3507 [Cqoi2014]通配符匹配
题意 几乎所有操作系统的命令行界面(CLI)中都支持文件名的通配符匹配以方便用户.最常见的通配符有两个,一个是星号("*"),可以匹配0个及以上的任意字符:另一个是问号(" ...
- C#网络编程(同步传输字符串) - Part.2
服务端客户端通信 在与服务端的连接建立以后,我们就可以通过此连接来发送和接收数据.端口与端口之间以流(Stream)的形式传输数据,因为几乎任何对象都可以保存到流中,所以实际上可以在客户端与服务端之间 ...
- 【转】数据库范式(1NF 2NF 3NF BCNF)
范式判断流程图 1. 四种范式之间关系 2.第二范式.第三范式.BCNF区别: 2NF:非主键列和主键列之间,是完全依赖于主键,还是依赖于主键的一部分(只依赖某个主键): 3NF:非主键列之间,不存在 ...
- PADS Logic 脚本的 Fields 一个对象记录
PADS Logic 脚本的 Fields 一个对象记录 PADS Laogic 有一个非常棒的脚本功能,可以导出所以元件. 我目前是把脚本绑定到 Ctrl+S 上,在保存时自动导出 txt 文件,方 ...
- 客户端如何调用WebService并且POST数据
直接上代码 using System; using System.Collections.Generic; using System.Linq; using System.Text;using Sys ...
- 对象的克隆,Dozer的使用
现在有个复杂对象bean需要在赋值后在另一个地方用,想通过复制的方式拿到这个对象.首选是深度克隆,虽然发现该对象的父类已经实现了Cloneable接口,但父类是通过jar包引入的,而且在clone方法 ...
- Java基础知识复习(一)
- JVM内存区域 程序计数器:一个比较小的内存区域,用于指示当前线程所执行的字节码执行到了第几行,是线程隔离的. Java虚拟机栈:Java方法执行的内存模型,用于存储局部变量,操作数栈,动态链接, ...
- Intent使用方法
显示Intent 通过构造函数的重载,创建Intent对象,并用startActivity()启动目标活动. 目标活动需要在AndroidManifest.xml中注册 ...... Intent i ...
- android开源库收集
1. google valley Android网络通信库 git clone https://android.googlesource.com/platform/frameworks/volley ...
- 【转】 Pro Android学习笔记(九六):AsyncTask(5):横竖屏切换问题
目录(?)[-] 横竖屏切换的问题 WeakReference 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flow ...