You already created a database after running your Code-First application the first time, but what about the second time onwards?? Will it create a new database every time you run the application? What about the production environment? How do you alter the database when you change your domain model? To handle these scenarios, you have to use one of the database initialization strategies.

There are four different database initialization strategies:

  1. CreateDatabaseIfNotExists: This is default initializer. As the name suggests, it will create the database if none exists as per the configuration. However, if you change the model class and then run the application with this initializer, then it will throw an exception.
  2. DropCreateDatabaseIfModelChanges: This initializer drops an existing database and creates a new database, if your model classes (entity classes) have been changed. So you don't have to worry about maintaining your database schema, when your model classes change.
  3. DropCreateDatabaseAlways: As the name suggests, this initializer drops an existing database every time you run the application, irrespective of whether your model classes have changed or not. This will be useful, when you want fresh database, every time you run the application, like while you are developing the application.
  4. Custom DB Initializer: You can also create your own custom initializer, if any of the above doesn't satisfy your requirements or you want to do some other process that initializes the database using the above initializer.

To use one of the above DB initialization strategies, you have to set the DB Initializer using Database class in Context class, as shown below:

Database Initialization Strategies in Code-First:的更多相关文章

  1. Entity Framework Code-First(17):Database Initialization Strategy

    Database Initialization Strategies in Code-First: You already created a database after running your ...

  2. 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 ...

  3. 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 ...

  4. Entity Framework Code-First(6):Database Initialization

    Database Initialization: We have seen that Code First creates a database automatically in the Simple ...

  5. EntityFramework6.X 之 Database Initialization

    Database Initialization 下图是数据库初始化的工作流 EF为数据库初始化准备了多种策略: l  CreateDatabaseIfNotExists:这是默认的初始化策略 l  D ...

  6. 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] ...

  7. java Class的Long id初始化 为0的问题android数据库操做出现的 android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed

    java的class中的Long类型变量调用默认的 构造函数new后会被初始化为0. 这句话大家可能感觉这么低级的事情还用你说? 我想说的是这个会产生的一个应用场景 和 避免方法 场景:db插入时候p ...

  8. Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null, configuration:{use:database=default}) (state=08S01,code=0)

    sparksql 2.和hive2.1.1 由于sparksql中的hive-cli 等包的版本是1.2的需要自己下载,下载替换之后不报错,替换之前做好备份

  9. Entity Framework Code-First(20):Migration

    Migration in Code-First: Entity framework Code-First had different database initialization strategie ...

随机推荐

  1. Entity Framework Code First (七)空间数据类型 Spatial Data Types

    声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...

  2. poj1984 带权并查集(向量处理)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5939   Accepted: 2 ...

  3. java-io-FileReader和FileWriter类

    实例:用FileWriter类向文件中写入一个串字符,然后用FileReader读出写入的内容. import java.io.*; public class FileStream2{ public ...

  4. jquery提交表单,回调函数

    1.使用jquery 绑定事件,执行以下代码,提交单并使用回调函数 $form.serialize():serialize()序列号表单参数 var $form = $("#form&quo ...

  5. RunLoop的深入了解

    RunLoop 是 iOS 和 OS X 开发中非常基础的一个概念,这篇文章将从 CFRunLoop 的源码入手,介绍 RunLoop 的概念以及底层实现原理.之后会介绍一下在 iOS 中,苹果是如何 ...

  6. Debugger 怎么用

    Debugger 是一个很有用的工具,尤其对于workflow开发人员来说.可以帮助我们check 从source 到target的数据流.我们可以看到什么数据从source中流出,在接下来的tran ...

  7. Maven配置不成功

    配置了MAVEN_HOME,新建了java文件,在d:/java/MAVEN_HOME/apach....,path下输出%MAVEN_HOME%bin,为什么cmd下mvn不行呢?因为MAVEN_H ...

  8. PowerDesigner设计表时显示注释选项

    选定编辑的表,[右键]->[Properties]->[Columns]->[Customize Columns and Filter]->[Comment]->[OK] ...

  9. SQLChop、SQLWall(Druid)、PHP Syntax Parser Analysis

    catalog . introduction . sqlchop sourcecode analysis . SQLWall(Druid) . PHP Syntax Parser . SQL Pars ...

  10. Multiples of 3 and 5

    #include<stdio.h> int main(void){ int n1, n2,n3; n1=333*(3+999)/2; n2=199*(5+995)/2; n3=66*(15 ...