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. sql-将查询字段拼接起来

    sql语句 select (a + b) as c mysql不起作用

  2. 22 java当中的数组

    class Test { public static void main(String args[]) { //数组的静态声明法 int arr[]={5,2,7,9,0}; //数组的动态声明法 i ...

  3. perl split 的一种特殊用法

    参考 http://blog.chinaunix.net/uid-1919528-id-2792055.html split 函数的正规语法应该是: split /PATTERN/, EXPR 而使用 ...

  4. bzoj2631: tree

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  5. ELF Executable Reconstruction From A Core Image

    catalog . INTRODUCTION . THE PROCESS IMAGE . THE CORE IMAGE . EXECUTABLE RECONSTRUCTION . FAILURES I ...

  6. 浅析基本AI五子棋算法

    五子棋是所有棋类博弈中比较简单的了,这里介绍的也只是一种非常基本的AI策略.其实,包括之前的AI贪吃蛇,感觉这两个AI其实体现的都是一种建模思想,把一个现实中的问题模型化,抽象化,得到其一般特征,再设 ...

  7. 【Alpha】团队贡献分配计划

    在仔细看过邹老师的博客和一些主流公司的绩效管理考核方面的内容后,本来我们小组在讨论后决定简化Google的OKR制度,加入一些自己的元素作为我们团队的主要贡献评定制度. OKR就是“目标和关键成果”( ...

  8. oracle创建存储过程

    创建: create or replace procedure insert_tbuser (   username nvarchar2,usersex nvarchar2,userage numbe ...

  9. Code笔记之:CSS+HTML display 属性

    display属性值:none 此元素不会被显示. block 此元素将显示为块级元素,此元素前后会带有换行符. inline 默认.此元素会被显示为内联元素,元素前后没有换行符. inline-bl ...

  10. 配置ASP.NET Web应用程序, 使之运行在medium trust

    这文章会向你展示, 怎么配置ASP.NET Web应用程序, 使之运行在medium trust.   如果你的服务器有多个应用程序, 你可以使用code access security和medium ...