Entity Framework
Dotnet Core Entity Framework Migrations on Cloud Foundry
Run database migrations on Cloud Foundry using a task to ensure that migrations are only done once on deployment (and not during app start up!) for dotnet core applications. This post assumes you are deploying built applications and not raw source code.
Instead of depending on dotnet tooling (dotnet-ef
), leverage the built-in migration capability of the DataContext. Interrupt the startup procedure when a special argument is passed in, instead of starting the web application, run the migrations and then exit.
So here we go, to start we need a DbContext:
Multiple DbContext's with migrations on a single database
Here is a small problem I ran into while working on my current project. This project already has an Entity Framework DbContext
class with entities and a database. I’m adding a new feature but want to keep the changes as isolated as possible. So I wanted to create a second DbContext
that handles the entities specific to my new feature.
Adding the context to the codebase is no hassle, just create the new class and derive it from DbContext
. The problem is in the migrations. Entity Framework has a system that can track changes to the model and apply those changes to the actual database. To keep track of these changes, Entity Framework uses a table called __MigrationHistory
where it stores all kinds of metadata about the model that it can compare to when you make changes to your entities in code.