You already created a database after running your Code-first application the first time and what's about the second time on Wards?? Would it create a new database every time you run the application? What's the production environment? How does you alter the your domain model? To handle these scenarios, you have to use one of the database initialization strategies.
There is four different database initialization strategies:
- createdatabaseifnotexists: This is the default initializer. As the name suggests, it would create the database if none exists as per the configuration. However, if the model class and then run the application with this initializer, then it'll throw an exception .
- dropcreatedatabaseifmodelchanges: This initializer drops an existing database and creates a new database, if your model classes (entity classes) has been C Hanged. So you don ' t has to worry the maintaining your database schema, when your model classes change.
- dropcreatedatabasealways: As the name suggests, this initializer drops a existing database every time you run the application, irrespective of whet Her your model classes has changed or not. This is being useful, when you want fresh database, every time you run the application, and like while you are developing the A Pplication.
- 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 s ome 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 C Lass, as shown below:
Database initialization Strategies in Code-first: