Azure Source Control Deployment not running my latest Code First Migration - entity-framework

I have an Azure website being deployed using the source control deployment and am using EF Code First Migrations to manage my database schemas. Everything is set up and appears to be functioning properly and it deploys without errors, but there are only 4 migrations in my migration table in the database and there should be 5. My application is also throwing the following error when I try to hit the database
The model backing the 'dbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
indicating not all of the migrations have been run. Is there somewhere on Azure I can look to see more detailed log files on the deployment and does anyone know why the migrations would run locally but not on azure?

Add this to your db context contructor:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Data.Migrations.Configuration>());
note: change "MyContext" to the name of your context.

Related

EF Core: how to apply the last migration to another database?

I have 2 databases, development and production.
I added a new entity, added a migration then updated the development database.
It worked, development database has the new table.
I switched the database in my configuration to the production database.
I used Update-Database command from Package Manager Console but nothing happened.
My production database still doesn't have the new table.
What now?
What is the valid workflow for such scenario?
BTW, both databases already contain structure and data. The production database contains more recent data, the development database is one migration ahead.
UPDATE: I tried to revert the last migration on development database, it worked. Then I tried to apply it again on production. Didn't work. It seems like it refuses to apply the same migration again.
OK, I figured it out.
I assumed when I change the runtime profile for production the file appsettings.Production.json will be loaded on migration.
It didn't happen. The migration used appsettings.Development.json instead.
I changed the connection string in my appsettings.Development.json file to the production database and it worked.
It's weird, when I debug the web application it respects the settings. It either uses the development or production settings, as selected in Visual Studio. Migrations just use the development file.
If there's a cleaner way (IDK, tell the Update-Database command to use a different appsettings file), please let me know in comment.

Why explicitly run CLI commands outside EF applications to handle migration?

The documentation for Entity Framework says to use migration CLI commands to create a database that doesn't exist yet for our EF model, and sync a database when our EF model changes.
Why do we need to explicitly run CLI commands outside our application in order to handle migration?
Can our applications that use EF implicitly handle migration: create a database if it doesn't exist for our EF model and sync a database when our EF model changes?
I had a little experience with Hibernate before, and I didn't hear about migration then. I might be wrong but left with the impression that applications using Hibernate could handle migration implicitly.
You can do either one you want. If you have a formal DevOps deployment process you would normally deploy your database schema then, and the CLI commands are how you do that with Migrations. You can run the migration in the deployment pipeline, or use the CLI to generate the upgrade scripts and run the scripts in the deployment pipeline.
See
Some apps may want to apply migrations at runtime during startup or
first run. Do this using the Migrate() method. . . .
Warning
This approach isn't for everyone. While it's great for apps with a
local database, most applications will require more robust deployment
strategy like generating SQL scripts.
Apply migrations at runtime
So while you would normally apply migrations at runtime on your private developer database, for deployment to shared environments it't often not the best choice.

Workflow for updating latest Entity Framework Core migrations automatically

So I have an EF Core project. When I need to add/migrations I issue and update command:
dotnet ef database update -v
This all works fine, if I need to update the QA, Staging, Prod database I update the connection string in my appsettings.json file and run the command.
What happens if I don't have access to production from my local machine? How do you go about updating the database to the latest migration?
If I remember working with Entity Framework (not .net core). It would try and update the database automatically when I deploy a new version of the .net application. Does this functionality still exist in .NET core?
Yes you could run
dbContext.Database.Migrate();
On startup but it is generally not a good idea to conflate database migration with your application lifecycle - best practice is to keep your application start up as fast and simple as possible because an application start failure is hard to diagnose remotely, and a migration would introduce a lot of unwelcome complexity.
The alternative is to run a migration operation as part of your deployment. It depends on what method you use to deploy but say for example you use a CI server, you will be able to run
dotnet ef database update
after copying the new code but before starting the application back up.

How to automate Entity Framework database migration within VSTS continuous integration

I'm faced with this scenario: I want to release my software into production on Azure, but there's a code-first database migration that must be applied at the same time to an Azure SQL database. During the time that new software version is pushed without the new database schema (or vice-versa), there will be a period of time that software will throw the exception The model backing the 'BlogContext' context has changed since the database was created..
My software is deployed upon pushing git commits to a branch using continuous integration in Visual Studio Team Services, so I really need a way to run update-database at the same time.
It appears this can be done using a manual publish from Visual Studio by checking the Update Database box (below), but I need this to be automated.
If you do not care about the data just drop the dbo.__MigrationHistory table
Hope this helps.
Can you take direct control over the deployment process (Team Services can help, or Octopus, Jenkins, others)? If so, deploy the database ahead of the code. That's how I would do it if I wasn't using Entity Framework. I would assume the same even with Entity Framework.
An entity framework context is initialised, by default, using CreateDatabaseIfNotExists<TContext>. If the database schema is different than the EF model, then error The model backing the 'BlogContext' context has changed since the database was created. will be triggered.
By adding to your db context contructor Database.SetInitializer<context>(null); the model error will not be triggered. This means you can deploy schema changes to production, without causing model errors, ahead of the new version of the application being deployed that contains the new db context, which equals no down-time.
Make use of an appSetting so that production code will use the null initializer.

'context has changed since the database was created' issue while deploying to Windows azure

While working in my local, i added two columns in my model. Added migration for it and updated the database. It works fine in my local.
My production server is in windows azure. While deploying, i made sure to check the checkbox of 'Execute Code First Migrations (runs on application start)'. But i am still getting the following error:
The model backing the 'DataAccess' context has changed since the
database was created. Consider using Code First Migrations to update
the database (http://go.microsoft.com/fwlink/?LinkId=238269).
In many blogs i found this solution to be added in Global.asax:
Database.SetInitializer<Mycontext>(null);
But i have already an existing database with many records, so i fear it may initialise my database again. Can anyone help me in solving this issue?
You could try running the migrations locally against your production database. You'll only need to change the connection string to access your production database.