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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Answer was given in my post.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.