Database Initialization Strategies in Code-First:
2016-06-19 10:13
246 查看
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:
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:
相关文章推荐
- DataBinding框架初体验
- 以受限方式启动数据库
- 组合模式(Composite)—分公司与总部
- 附加作业
- CSS3变形之3D变形
- 简单几招破解Windows管理员密码
- L2-010. 排座位(并查集水题)
- 【Ionic实战】一个和AngularJS的跨平台(iOS,Android) APP框架
- 附加题
- 根据传入的数据绘制圆环,以及以绘画形式自定义图片为圆形
- [caffe]深度学习之图像分类模型AlexNet解读
- Linux 系统应用编程——网络编程(利用TCP/IP 模型分析数据传输过程)
- 沈阳航空软件工程附加
- [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
- 174. Dungeon Game
- 增量更新:bsdiff工具的安装和使用
- 上班之前的“踩点”--一个程序员的成长史(9)
- 【Ionic实战】自动升级Android APP
- linux dup函数源码剖析
- jstree的使用