In last few articles, I have been mostly writing about .NET Entity Framework Core. In almost every article, there is a demo and the demo source code was provided with the article.
In every article, we have almost same steps – create a class library which will contain EF Core context and then create migrations and apply them using dotnet CLI commands. Till now, I was really not interested looking inside the migrations generated after running those commands.
But today, I thought, it might be good idea to have a high level overview of what those files contain. And that’s why today’s article is discuss what is inside those migration files.
Before diving into the auto generated files, let’s first revise few concepts that we have seen in previous articles.
What are migrations ?
In real world applications, all the database design for an application does not happen overnight.
Almost all the projects start with some basic database design and then the design evolves over the time. Some columns may get added to some entities, some entities might lose some properties over time and some entities may get dropped over the time. Traditionally, all such modifications used to get applied via SQL scripts.
Migrations is the feature from EF Core which allows incrementally evolving the database. Generally below are the steps involved when any change is done in EF core data access layer:
- When a change is introduced, generally a developer uses one of the two tools mentioned below to add a new migration.
- While generating migrations, EF core compares the current model with snapshot of the old model to determine the differences.
- Based on the comparison done in previous step, EF core generates migration source code files. This files can be tracked in source control, like any other file.
- Once the migrations are generated, these migrations can be applied to the database.
- EF core uses a special history table – __EFMigrationsHistory – to track which migrations were already applied.
How to add a migrations ?
There are two ways to generate migrations:
- Using dotnet CLI tools (the recommended approach for .NET Core apps)
- Using NuGet package manager console (for EF6 – .NET Framework like experience or for working from Visual Studio Package Manager)
You can refer my previous article for code examples.
What does a migration contain ?
While adding a migration, a name must be specified. This name is the logical name of the modifications which would be applied via the migration. When a command is used to add a migration, it creates a Migrations folder if it does not already exist in the project directory.
In addition, adding a new migration also adds some files to the Migrations folder. In this case, let’s assume that the name of migration is MigrationName and context name is UniversityContext.
YYYYMMDDhhmmss_MigrationName.cs
This file contains the date and time prefix – which signifies the time at which this migration was created.
This is a file which contains two methods – Up and Down.
- Up method contains code which would modify database and apply the new changes
- Down method contains code which would rollback the new changes from database
Below is a sample of such file, which just adds a single table – Students – to a database – University.
YYYYMMDDhhmmss_MigrationName.Designer.cs
The migrations metadata file. Contains information used by EF.
UniversityContextModelSnapshot
The snapshot of your current model. This file is used to determine what changed when adding the next migration.
How to apply those migrations ?
Migrations can be applied by using various methods:
- Apply Using dotnet CLI tools (the recommended approach for .NET Core apps)
- Apply Using NuGet package manager console (for EF6 – .NET Framework like experience or for working from Visual Studio Package Manager)
- Apply At Runtime – this approach uses context.Database.Migration() API to apply the migration when the application is running. Usually, such applications apply migrations at very beginning, before running any functional code. This method may be suitable for initial stages during the development, but it is certainly not suitable for production applications.
Below are some commands which show how to add and apply the migration using dotnet CLI tools:
In addition to these commands, you can also refer this post for some more information about these commands. You can also choose apply migrations via SQL scripts.
How to reset the database ?
First two commands from the code snippet given above are actually to reset everything. First command deletes an existing database. Second command deletes all migrations from the Migrations folder. I find it easy because it involves running commands and there are no manual efforts.
Another way is to delete the database using Drop Database command and then delete Migrations folder manually.
If you want to play a bit with EF core history table, the documentation also provides another method for resetting the database – which involves playing with __EFMigrationsHistory table.
I hope you find this information helpful. Let me know your thoughts.