Is there a way to execute the pending migrations automatic after publishing. I know there was an option for this in EF6.
Related
I am using Entity Framework code first. I am able to update my database during development using Update-Database from Nuget Package Manager. Now I want to the database to be updated when building from my Azure Pipeline. Is there any documentation from Microsoft of this? How are others handling this?
(Not sure how much information you are looking for, so ask a follow-up/add a comment if this isn't enough.)
Server-side (e.g. in CI pipelines) as well as client-side, this can be done with the CLI instead of the VS specific package manager console:
dotnet ef database update --verbose
See Entity Framework Core tools reference - .NET Core CLI: dotnet ef database update for further information.
Generally, I would discourage anybody from automatically applying database migrations to a production environment.
Migrations should always be scripted to a file via dotnet ef migrations script, thoroughly checked and tested and only then be applied directly from this script (after creating a backup) to a production database.
I have an asp.net core web api with a connection to a postgres db. I added a migration via dotnet ef cli and updated my database via:
dotnet ef migrations add initial
dotnet ef database update
There was no problem and I found my newly generated tables and the migration history in my schema. Then I messed up and did the same from a different project with a different name but with the same connection string. I used the same commands as above.
Now I cannot access neither the migration history table nor the generated tables anymore:
ERROR: must be owner of relation __EFMigrationsHistory
I wasn't able to get the rights for my migration history again.
How do I reset this to square one? Thank you in advance.
My company is moving to microservices and as part of this shift devops is standing up CI/CD build release pipelines in Azure with VSTS and GIT.
The current model for managing migrations is a developer is given 2 projects in 2 separate git repositories.
Project 1 - API Services project - .NET Framework / .Net Core
Project 2 - Database project based on EF6 using the migration API
These projects have completely independent release pipelines based on the repositories. So when you create a pull request into master the pipeline builds and releases the project.
This new architecture also supports blue green deployments and our app services run on multiple nodes.
The issue we have is that with this set up we have to basically hand code our migrations and can't use any of the tooling provided in EF Core.
Most of the articles and documentation I have read shows running the migrations from app start up, but if you have multiple app service nodes how do you prevent 2 nodes from running the migrations?
Other articles I have looked at show moving migrations into a separate project, but that project needs a reference to the project with the dbcontext in it. In my company's setup this is not possible. Neither can we do the reverse since moving the dbcontext into the database project prevents us from referencing it in the api services project.
Is there any way to support this model with EF Core?
What is the preferred way to implement blue green deployments with EF Core Migrations on a multi node app service?
I will try to claim that there isn't and not because EF Core doesn't support it in some way, but because this sounds impossible from what I understood in your question.
Your company want the ability to do blue/green deployments, but it is only really possible on the service layer, but not on database. The idea sounds really cool, fast rollback, almost no downtime. But in reality databases complicate things a lot.
So imagine your Project 1 is running on machines A and B (representing blue and green deployments). A currently is a production environment and B is identical but not serving any requests ATM. Both of them are pointing to the exact same database (if not, it's not blue/green, it's just a separate environment). When you want to deploy your DB changes, you migrate your database, but now both of the machines A and B will be pointing to the updated database. You can keep switching from A to B, but they both might have stopped working if your database migration broke anything.
Therefore I don't really understand what you achieve with having DB migrations in a separate repository with a separate pipeline. This just complicates coordination of the releases as they are clearly dependent, but I don't see how it helps to solve anything. As you noted, you can't delegate creation of migration scripts to EF Core, at least without some manual work.
Would be happy to hear any advantages of such design.
I have previously used DbUp for database migrations. One of the features that I liked was that it could wrap all of the migration scripts in a single transaction. So if any of the migration scripts failed, any previously executed migration scripts would be rolled back.
Can Entity Framework 6 wrap all migrations with a single transaction?
With EF Migrations you can add migration scaffoldings and then keep running 'Update-Database' to apply changes. And in order to rollback the following works:
Update-Database -TargetMigration: <xyzMigration>
This is great for updating your dev. DB. However when I automate migrations using MigrateDatabaseToLatestVersion for deploying to other environments like test and prod. adding migration files for any changes and having them reflect works well.
But, in case I wanted to rollback to a specific migration how do I achieve that? 'Update-Database -TargetMigration:' only updates your local dev. DB or whatever your connection string is pointing to locally. And that has to be run via Package Manager Console. Is rollback not an option for actual deployments? Do you have to just 'Add-Migration' and specifiy a new migration that has all the changes you want reflected?
For a production database doing an Add-Migration with the changes you want would probably make the most sense, especially if you have multiple deployments.
Other options include:
Using DbMigrator.Update() with a parameter that indicates the target migration. (I'd probably use a separate build, just for this purpose...seems tricky to handle...especially when you add additional migrations in the future).
Update-Database also has optional arguments for -ConnectionString, but that would require setting up Visual Studio on a machine with access to the database you're targeting.