Database Initialization Strategies in Code-First:
Posted 吴晓阳
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:
以上是关于Database Initialization Strategies in Code-First:的主要内容,如果未能解决你的问题,请参考以下文章
spring Boot启动报错Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.s
C++ static & dynamic initialization
DELPHI连接数据库错误:vendor initialization failed