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 ...
随机推荐
- SqlServer 数据库读写分离【转】
1. 实现原理:读写分离简单的说是把对数据库读和写的操作分开对应不同的数据库服务器,这样能有效地减轻数据库压力,也能减轻io压力.主数据库提供写操作,从数据库提供读操作,其实在很多系统中,主要是读的操 ...
- 【4】JDK和CGLIB生成动态代理类的区别
当一个对象(客户端)不能或者不想直接引用另一个对象(目标对象),这时可以应用代理模式在这两者之间构建一个桥梁--代理对象. 按照代理对象的创建时期不同,可以分为两种: 静态代理:事先写好代理对象类,在 ...
- rpm -Uvh 升级时的陷阱
问题现象 用rpm -Uvh升级后,原先的一个软链接被删除了,而采用先rpm -e 卸载rpm包,再rpm -ivh 安装包的方法,这个软链接还在.这个软链接是在rpm包安装的时候建立,也只有在rpm ...
- [转]HTTP 协议中的 Transfer-Encoding
本文作为我的博客「HTTP 相关」专题新的一篇,主要讨论 HTTP 协议中的 Transfer-Encoding.这个专题我会根据自己的理解,以尽量通俗的讲述,结合代码示例和实际场景来说明问题,欢迎大 ...
- bzoj 1415 [Noi2005]聪聪和可可——其实无环的图上概率
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1415 乍一看和“游走”一样.于是高斯消元.n^2状态,复杂度n^6…… 看看TJ,发现因为聪 ...
- mysql链接出现10060
http://www.cnblogs.com/meetrice/p/5309666.html 使用navicate链接 http://www.cnblogs.com/cxint/p/7454054.h ...
- 如何让公司从SVN改到Git?
把公司的SVN迁移到GitLab CE(GitLab社区版)原因主要有下面几个: 年青的新人进来,喜欢用git的越来越多 GitLab CE提供了优美的 web 界面,图形化分支结构,更直观的代码审查 ...
- 【转】JMeter基础之——一个简单的性能测试
上一节中,我们了解了jmeter的一此主要元件,那么这些元件如何使用到性能测试中呢.这一节创建一个简单的测试计划来使用这些元件.该计划对应的测试需求. 1)测试目标网站是fnng.cnblogs.co ...
- 网络编程Socket之wireshark使用
这里只对wireshark进行简单的使用介绍.里面的报表,IO图形分析等以后再谈.... 这里不提供下载地址了,不是什么稀有资源,我使用的wireshark是在百度软件中心下载的. 第一步:选择需要 ...
- xunsearch进阶使用
目录 设置分页 设置排序 读取文档结果 搜索结果高亮处理 获取数量 获取热门搜索词 获取相关搜索词 设置分页 $search->setLimit(5); // 设置返回结果为前 5 条 $sea ...