Database initialization Strategies in Code-first:

Source: Internet
Author: User

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:

  1. 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 .
  2. 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.
  3. 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.
  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 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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.