Entity Framework Code-First(17):Database Initialization Strategy
Database Initialization Strategies in Code-First:
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:
- 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.
- 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.
- 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.
- 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:
public class SchoolDBContext: DbContext
{ public SchoolDBContext(): base("SchoolDBConnectionString")
{
Database.SetInitializer<SchoolDBContext>(new CreateDatabaseIfNotExists<SchoolDBContext>()); //Database.SetInitializer<SchoolDBContext>(new DropCreateDatabaseIfModelChanges<SchoolDBContext>());
//Database.SetInitializer<SchoolDBContext>(new DropCreateDatabaseAlways<SchoolDBContext>());
//Database.SetInitializer<SchoolDBContext>(new SchoolDBInitializer());
}
public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; }
}
You can also create your custom DB initializer, by inheriting one of the initializers, as shown below:
public class SchoolDBInitializer : CreateDatabaseIfNotExists<SchoolDBContext>
{
protected override void Seed(SchoolDBContext context)
{
base.Seed(context);
}
}
As you can see in the above code, we have created a new class SchoolDBInitializer, which is derived from DropCreateDatabaseAlways initializer.
Set db initializer in the configuration file:
You can also set db initializer in the configuration file. For example, to set default initializer in app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DatabaseInitializerForType SchoolDataLayer.SchoolDBContext, SchoolDataLayer"
value="System.Data.Entity.DropCreateDatabaseAlways`1[[SchoolDataLayer.SchoolDBContext, SchoolDataLayer]], EntityFramework" />
</appSettings>
</configuration>
You can set custom DB initializer, as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DatabaseInitializerForType SchoolDataLayer.SchoolDBContext, SchoolDataLayer"
value="SchoolDataLayer.SchoolDBInitializer, SchoolDataLayer" />
</appSettings>
</configuration>
This way, you can use DB initialization strategy for your application.
Entity Framework Code-First(17):Database Initialization Strategy的更多相关文章
- Entity Framework Tutorial Basics(13):Database First
Database First development with Entity Framework: We have seen this approach in Create Entity Data M ...
- Entity Framework Tutorial Basics(17):DBSet Class
DBSet Class DBSet class represents an entity set that is used for create, read, update, and delete o ...
- Entity Framework Code first(转载)
一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...
- Entity Framework Code First (三)Data Annotations
Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...
- Entity Framework Code First (二)Custom Conventions
---------------------------------------------------------------------------------------------------- ...
- Entity Framework Code First (一)Conventions
Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...
- Entity Framework Tutorial Basics(4):Setup Entity Framework Environment
Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...
- Entity Framework Tutorial Basics(11):Code First
Code First development with Entity Framework: Entity Framework supports three different development ...
- Entity Framework Code First (七)空间数据类型 Spatial Data Types
声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...
随机推荐
- 14_ Component 游戏开发组件模式
# component 不同作用的程序需要保持互相隔离 我们不想ai 物理 渲染 声音 等等功能 耦合在一起,像下面这样 ``` //bad if (collidingWithFloor() & ...
- python并发编程的几种方法
1.多线程 Threading模块 2.2.6版本以后 多进程 multiprocessing模块 3.协程 gevent (是一个基于libev的并发库.它为各种并发和网络相关的任务提供了整洁的AP ...
- BZOJ3832 Rally
传送门(权限) 题目大意 给定一个有向无环图,可以删去一个点和所有与它相连的边,使得图的其余部分最长路径最小,求这个位置和最小的最长路径长度. 题解 对于每一条边$u\rightarrow v$,设$ ...
- [ Laravel 5.5 文档 ] 数据库操作 —— 在 Laravel 中轻松实现分页功能
 简介 在其他框架中,分页是件非常痛苦的事,Laravel 让这件事变得简单易于上手.Laravel 的分页器与查询构建器和 Eloquent ORM 集成在一起,并开箱提供方便的.易于使用的.基于 ...
- bzoj 1798 [Ahoi2009]Seq 维护序列seq ——线段树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1798 先乘后加,就可给加法标记乘上乘法标记. 注意可能有 *0 的操作,所以 pshd 时不 ...
- 异常[PersistenceUnit: default] Unable to build Hibernate SessionFactory
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManage ...
- HDU4135(容斥原理)
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- Oracle 静默安装 oracle 12c
[oracle@local12cdg app]$ id oracleuid=501(oracle) gid=501(oinstall) groups=501(oinstall),502(dba)[or ...
- 转:面对一个全新的环境,作为一个Oracle DBA,首先应该了解什么
转载:面对一个全新的环境,作为一个Oracle DBA,首先应该了解什么 由于时间比较久了,找不到原文章的链接,先贴上内容供大家参考 在这里,不谈那些大的方面,比如了解整个IT环境整体情况,假设你已经 ...
- highcharts图表的图例legend
一.将图例Legend放于图表右侧1.设置chart的marginRight属性值:chart: { marginRight: 120}2.设置legend图例属性值如下 legend: { alig ...