Workflow for updating latest Entity Framework Core migrations automatically - entity-framework-core

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.

Related

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.

Automating a FluentMigrator Rollback on Azure Devops

Currently have a release pipeline that runs my migration project in the up direction -
e.g.
The web app project is deployed to the environment
In the Solution we have a fluent migrator project
As part of the release pipeline, we run the up migrations to the latest version
All of the above works great BUT if I want to rollback the web app to an earlier version, then I need to somehow pass into the fluent migrator process the version that I want to roll back to - currently I'm not sure how I would achieve this. It's almost like I would need to know the version that was deployed in the previous release.
Currently, I rollback the web app I have to manually run fluent migrator to rollback to the version of the database I require.
Has anyone fully automated the fluent migrator rollback?
FYI My migration numbers are using the datetime as milliseconds which I get from https://currentmillis.com/
Update:
I had a plan to somehow get the latest migration in the project and use that number as a parameter to either run up to it or down to it. However, after thinking it through, the migrations that run as part of the release only know about the migrations that exist when the code in that release is built. There's no way it would know about any consequent migrations to be able to roll the database back...
I think I would somehow need to pull the latest code, build it and then run down the appropriate migration. I'm not sure this is possible. Might have to stick to a manual database roll back procedure.
A less-than-elegant solution would be to append all migration identifiers to a file that is part of deployment artifacts. This way the forward migration uses the last entry, the backward migration uses the next-to-last entry.

How to label a migration as included on a newly-created DB

I have a team developing a web application using EF. They all have local copies of their DB, as well as there being a central DB for a published version of the app. Some of the devs, and the CI server, have built their initial DB from a later version of the model than others, and so when they run Update-Database or the migrate.exe tool, they get errors when running migrations they don't actually need.
Other than blowing away all DBs and starting over, what's the best way to get everyone on the same page migrations-wise? Can migrate or Update-Database be told to mark migrations as being applied even if they fail, and continue?

EF6 code-first migrations: how to deploy to staging environment the right way?

I have been following a number of tutorials regarding code-first migrations and am now at the stage where I am ready to deploy to our staging server.
We normally publish web apps to the filesystem and then manually update sites through Remote Desktop (not the greatest I know).
All the tutorials and best practises as far as code first and deployment go seem to be either out of date of specific to Azure deployment.
What is the current best practise for deploying a web app that has been developed with code first migrations (EF6) to a live environment? How then are updates to the live environment handled?
I understand that I can generate scripts using Update-Database but then these do not include any Seed Data. Are scripts the way to go?
Thanks,
You can use DBMigrator update menhod - this will run any pending migrations. The Seed method in your Configuration class will run every time your application starts.
You can also use migrate.exe to run database updates.

Deploy Entity Framework Code First

I guess I should have thought of this before I started my project but I have successfully built and tested a mini application using the code-first approach and I am ready to deploy it to a production web server.
I have moved the folder to my staging server and everything works well. I am just curious if there is a suggested deployment strategy?
If I make a change to the application I don't want to lose all the data if the application is restarted.
Should I just generate the DB scripts from the code-first project and then move it to my server that way?
Any tips and guide links would be useful.
Thanks.
Actually database initializer is only for development. Deploying such code to production is the best way to get some troubles. Code-first currently doesn't have any approach for database evolution so you must manually build change scripts to your database after new version. The easiest approach is using Database tools in VS Studio 2010 Premium and Ultimate. If you will have a database with the old schema and a database with the new schema and VS will prepare change script for you.
Here are the steps I follow.
Comment out any Initialization strategy I'm using.
Generate the database scripts for schema + data for all the tables EXCEPT the EdmMetadata table and run them on the web server. (Of course, if it's a production server, BE CAREFUL about this step. In my case, during development, the data in production and development are identical.)
Commit my solution to subversion which then triggers TeamCity to build, test, and deploy to the web server (of course, you will have your own method for this step, but somehow deploy the website to the web server).
You're all done!
The Initializer and the EdmMetadata tables are needed for development only.