This article is a mirror article of machine translation, please click here to jump to the original article.

View: 37510|Reply: 1

[Source] Database initialization strategy in EF 6 Code-First

[Copy link]
Posted on 4/17/2019 3:56:46 PM | | |
You have already created a database after running the Code-First application for the first time, but what is it after starting the second time? Is a new database created every time the application is run? What about the production environment? How do I change the database when I change the domain model? To handle these scenarios, you must use one of the database initialization policies.

There are four different database initialization strategies:

  • CreateDatabaseIfNotExists:This isdefaultinitialization procedure. As the name suggests, it will create a database if it does not exist according to the configuration. However, if you change the model class and then run the application with this initializer, an exception is thrown.
  • DropCreateDatabaseIfModelChanges: If your model class (entity class) has changed, this initializer will delete the existing database and create a new one. So you don't have to worry about maintaining database schemas when model classes change.
  • DropCreateDatabaseAlways: As the name suggests, this initializer deletes the existing database every time you run your application, regardless of whether your model class has changed or not. This is useful when you need a new database every time you run an application, for example when developing an application.
  • Custom database initializer: You can also create your own custom initializer if the above does not meet your requirements, or if you want to use the above initializer to perform the process of initializing other databases.

To use one of the above DB initialization strategies, you must set the DB Initializer using the class in the Database context class, as shown below:




You can also create a custom database initializer by inheriting one of the initializers, as shown below:




In the example above, the SchoolDBInitializer is a custom initializer class derived from CreateDatabaseIfNotExists. This separates the database initialization code from the context class.

Set up the DB Initializer in the configuration file

You can also set the db initializer in the configuration file. For example, to set the default initial value setting in app.config:




You can set up a custom database initializer as follows:




Close DB Initializer

You can turn off the database initializer for your application.Assuming you don't want to lose existing data in production, you can close the initializer, as follows:



You can also turn off the initializer in the configuration file, for example:








Previous:Illustrated Cryptography 3rd Edition
Next:Silicon Valley April 2018 employment class video
 Landlord| Posted on 2/7/2021 3:08:59 PM |
Allow Entity Framework to start no longer valid __MigrationHistory table
https://www.itsvse.com/thread-3255-1-1.html

Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com