EntityFramework6.X 之 Database Initialization
Database Initialization
下图是数据库初始化的工作流

EF为数据库初始化准备了多种策略:
l CreateDatabaseIfNotExists:这是默认的初始化策略
l DropCreateDatabaseAlways:总是创建数据库,不管数据库中是否存在数据,每次应用程序启动时就会重新设置
l DropCreateDatabaseIfModelChanges:有且仅当领域模型发生改变的时候创建数据库
若要启动测试数据则必须在应用程启动的时候通过Database.SetInitializer(new DropCreateDatabaseAlways<DBContext>())来初始化。
数据库初始化也可以继承最原始IDatabaseInitializer<>类来创建经典原始的初始化
public class AppTestContextCustomInitializer : IDatabaseInitializer<ApplicationDbContext>{
public void InitializeDatabase(ApplicationDbContext context){
if (context.Database.Exists()){
if (!context.Database.CompatibleWithModel(true)){
context.Database.Delete();}}
context.Database.Create();
context.Database.ExecuteSqlCommand("CREATE TABLE GLOBAL_DATA([KEY] VARCHAR(50), [VALUE] VARCHAR(250))");}}
可以通过配置文件来指定初始化数据库类
在<appSettings>节点中插入一个子节点:
<appSettings>
<add key="DatabaseInitializerForType CodeFirstDbInitializerDemo.BlogContext,CodeFirstDbInitializerDemo"
value="CodeFirstDbInitializerDemo.BlogContextCustomInitializer, CodeFirstDbInitializerDemo" />
</appSettings>
在模型中可用特性[Table(“tablename”)]指定存储的表格名称
EF中使用DbConfiguration类可定义一个Db配置类,使其继承DbConfiguration类,然后自定义Db配置,最后具体应用到具体的DbContext类上通过DbConfigurationType特性
如先创建一个配置类:
public class MyConfiguration : DbConfiguration{
public MyConfiguration(){
SetExecutionStrategy("providerName",()=>new SqlAzureExecutionStrategy());
SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0"));}}
使用的话可以通过两种方式:
一种通过配置文件进行:
<entityFramework codeConfigurationType="MyNamespace.MyDbConfiguration, MyAssembly">
...Your EF config...
</entityFramework>
还有一种通过特性配置:
[DbConfigurationType(typeof(MyDbConfiguration))]
publicclassMyContextContext : DbContext
{
}
EntityFramework6.X 之 Database Initialization的更多相关文章
- Database Initialization Strategies in Code-First:
You already created a database after running your Code-First application the first time, but what ab ...
- Database Initialization Parameters for Oracle E-Business Suite Release 12 (文档 ID 396009.1)
In This Document Section 1: Common Database Initialization Parameters For All Releases Section 2: Re ...
- Database Initialization Parameters for Oracle E-Business Suite Release 12
In This Document Section 1: Common Database Initialization Parameters For All Releases Section 2: Re ...
- Entity Framework Code-First(17):Database Initialization Strategy
Database Initialization Strategies in Code-First: You already created a database after running your ...
- Entity Framework Code-First(6):Database Initialization
Database Initialization: We have seen that Code First creates a database automatically in the Simple ...
- What is the Database Initialization Parameter That is Associated to an ORA-32004 Error ?
APPLIES TO: Oracle Database - Enterprise Edition - Version 9.2.0.1 to 11.2.0.3 [Release 9.2 to 11.2] ...
- 【原】Configuring Oracle Data Guard In Physical Standby Database
作者:david_zhang@sh [转载时请以超链接形式标明文章] http://www.cnblogs.com/david-zhang-index/p/5042640.html参照文档:https ...
- EBS initialization parameters - Healthcheck
APPLIES TO: Oracle EBS Applications Performance - Version 11.5.10.2 to 12.2 [Release 11.5.10 to 12.2 ...
- Creating a Physical Standby Database 11g
1.Environment Item Primary database standby database Platform Redhat 5.4 Redhat 5.4 Hostname gc1 gc2 ...
随机推荐
- [Linux] PHP程序员玩转Linux系列-Nginx中的HTTPS
1.PHP程序员玩转Linux系列-怎么安装使用CentOS 2.PHP程序员玩转Linux系列-lnmp环境的搭建 3.PHP程序员玩转Linux系列-搭建FTP代码开发环境 4.PHP程序员玩转L ...
- JavaScript Break 和 Continue 语句
1.break:终止本层循坏,继续执行本次循坏后面的语句: 当循坏有多层时,break只会跳过一层循坏 2.continue:跳过本次循坏,继续执行下次循坏 对于for循环,continue执行后,继 ...
- 读书笔记 effective c++ Item 45 使用成员函数模板来接受“所有兼容类型”
智能指针的行为像是指针,但是没有提供加的功能.例如,Item 13中解释了如何使用标准auto_ptr和tr1::shared_ptr指针在正确的时间自动删除堆上的资源.STL容器中的迭代器基本上都是 ...
- kindeditor修改图片上传路径-使用webapi上传图片到图片服务器
kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 在这里我着重介绍一些使用kindeditor修改图片上传路径并通过webapi上传图片到图片服务器的方案. 因为我使用的 ...
- linux命令之crontab定时执行任务
一.crond简介 crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务 工具,并且会自动启动c ...
- position relative
position的默认值是static,(也就是说对于任意一个元素,如果没有定义它的position属性,那么它的position:static) 如果你想让这个#demo里的一个div#sub相对于 ...
- PHP的laravel框架后台实现数据导出excel的功能
要想在PHP后台实现excel导入导出功能,一种简单有效的方法就是使用phpexcel插件. 要使用phpexcel插件,首先需要下载composer,这个工具是专门用来管理项目中库之间的依赖关系的. ...
- java构造代码块,构造函数和普通函数的区别和调用时间
在这里我们谈论一下构造代码块,构造函数和普通函数的区别和调用时间.构造代码块:最早运行,比构造函数运行的时间好要提前,和构造函数一样,只在对象初始化的时候运行.构造函数:运行时间比构造代码块时间晚,也 ...
- Python、PyCharm的安装及使用方法(Mac版)
上周跟朋友喝咖啡时聊起我想学Python,她恰好也有这个打算,顺便推荐了一本书<编程小白的第1本Python入门书>,我推送到Kindle后,随手翻看了下,用语平实,简洁易懂. 之前在R语 ...
- mySql 安装教程
看了好久别人的文章,今天就开始自己写第一篇.希望给别人能提供帮助,也可以方便自己查阅. 前两天自己安装了mysql,感觉是比oracle好装多了. mysql安装有两种方式,一种是安装包安装方式,一种 ...