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. java-URLConnection网络数据收取

    通过url创建connection方式收取 import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import ja ...

  2. java中是否会存在内存泄漏

    会.java导致内存泄露的原因很明确:长生命周期的对象持有短生命周期对象的引用就很可能发生内存泄露,尽管短生命周期对象已经不再需要,但是因为长生命周期对象持有它的引用而导致不能被回收,这就是java中 ...

  3. 【HDU 4305】Lightning(生成树计数)

    Problem Description There are N robots standing on the ground (Don't know why. Don't know how). Sudd ...

  4. Java多线程与并发库高级应用-传统线程同步通信技术

    面试题: 子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着又 主线程循环100次,如此循环50次,请写出程序 /** * 子线程循环10次,接着主线程循环100次,接着又回到 ...

  5. asp.net使用signalr实现集群集群下面的消息推送

    1.选用Signalr的原因 Signalr内部给我们做了很多封装.当服务器或者浏览器不支持websoket协议的时候就使用长连接方式  不支持长连接再选用轮询的方式获取消息 websoket:与服务 ...

  6. HDU 2795 Billboard

    Description 在学校的入口处有一个巨大的矩形广告牌,高为h,宽为w.所有种类的广告都可以贴,比如ACM的广告啊,还有餐厅新出了哪些好吃的,等等..   在9月1号这天,广告牌是空的,之后广告 ...

  7. phpMyadmin /scripts/setup.php Execute Arbitrary PHP Code Via A Crafted POST Request CVE-2010-3055

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 The configuration setup script (aka scrip ...

  8. java+tomcat(apr,native)

    #pdd 2014_12-24#安装java环境rpm -ivh jdk-7u72-linux-x64.rpm vim /etc/profile #set for java export JAVA_H ...

  9. 验证时出错。HRESULT = '8000000A'

    往往出现在 做了安装项目后有警告: 解决办法: 这本来是在VS2005下创建的一下项目,后来改用VS2010的开发环境,.NET Framework的版本还是使用2.0, 但每次生成之后都会在解决方案 ...

  10. linux系统安装jdk

    OpenJdk8安装: sudo add-apt-repository ppa:openjdk-r/ppa sudo apt-get update sudo apt-get install openj ...