EF6 MVC5 and Visual-Studio 2017 Code First Database Part II

I would like to continue discussing this project as a new question.  Particularly regarding the model/data migration with PMC vs. DropCreateDatabaseAlways and how to mix or use one or the other.

At present the project referenced in the question:
EF6 MVC5 and Visual Studio 2017 Code First Database - Part I

uses a migration folder, configuration.cs for seeding, the PMC for doing Update-Database to update model and add seed data.

I would like to transition to using the JobInitializer class where possible.  I know that there are references to it possible through the web.config in the entityframework section,  global.asax and/or the PMC with a enable-migration command which includes references to context and the Initializer class.

Any help in unraveling all this would be appreciated.
LVL 1
Howard BashSenior Software EngineerAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Hi Howard;

To your question, " Particularly regarding the model/data migration with PMC vs. DropCreateDatabaseAlways and how to mix or use one or the other.". Prior to Entity Framework 4.3 Code First used a database initialization strategies which had four options.
From Database Initialization Strategies in Code-First
1. 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.

2. 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.

3. 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.

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 some other process that initializes the database using the above initialize
The problem with the above database-initialization-strategies is that it was a delete all and restart with a new database which you may not always want.

Database initialization from Entity Framework 4.3 and above is called Migration
From Migration in Code-First
Entity framework 4.3 has introduced a migration tool that automatically updates the database schema, when your model changes without losing any existing data or other database objects. It uses a new database initializer called MigrateDatabaseToLatestVersion.
Please see the link above on Migration in Code-First for two forms, Automated  and Code based Migration.

You want to use one or the other but you don't want to try and use both at the same time.
0
 
Fernando SotoRetiredCommented:
Answer was given in my post.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.