When working with a fast-evolving project changes happen rapidly not only to the project’s code but also to the database schema. It happens especially when working on a micro-service from the start when its purpose shifts.
In Entity Framework Core 5 migrations we are adding a migration as a delta changes between our DbContext
class and existing [DbContext name]ModelSnapshot
. When generating new migration a CLI Tool will generate only the differences between those two and put them in two methods: Up
and Down
. In the first one, there will be a change to apply the migration and in the second one, to remove the migration.
After the migration is applied, its name is noted in the __EFMigrationsHistory
table.
Let’s say that after changing a schema multiple times in the early stages our project is now stable. We have a bunch of migrations that could be merged into one, that would create a model once, without many small updates.
The easiest way to merge all migrations would be removing everything! What I mean is:
__EFMigrationHistory
tableThis is a drastic way of merging migrations because we will lose all the data. However, it’s super simple and it might work in some cases.
This means that we cannot remove all already created database objects, but we can merge migration files in our code. Let’s see how that can be done.
dotnet ef migrations add MergedMigration
Up
and Down
methodsMergedMigration
migration with command dotnet ef database update
MergedMigration
file with earlier generated code#asp.net core for .net 5 & ef core 5 #ef core 5 #ef core migrations #primehotel