Updating a database using a migration in EF Core is the process of applying changes to a database using the Up
method from the migration file or reverting changes using the Down
method. To apply or unapply a migration, the Update Command
must be run. This command checks the database for all applied migrations and will execute one of the two following processes:
- Apply a migration: The Up Method will be executed if the specific migration is more recent than the latest applied migrations.
- Revert a migration: The Down Method will be executed if the specific migration is older than the latest applied migrations.
There are 2 ways to run the Update Database
command and both will be covered in this article:
Update-Database
: When using the Package Manager Console (PMC)dotnet ef database update
: When using the .NET Command Line Interface (.NET CLI)
The Update Database
command is one of the 3 main migration commands:
- Add Migration
- Update Database
- Remove Migration
How to use the Update-Database command with PMC in EF Core?
To use the Update-Database
command with the Package Manager Console (PMC) in EF Core:
- Open the Package Manager Console in Visual Studio.
- Ensure the default project in the console is the one containing your EF Core DbContext.
- Enter the following command:
Update-Database
For applying a specific migration or reverting to a previous migration, you must specify the migration name:
Update-Database [MigrationName]
NOTE: Replace [MigrationName]
with the name of the migration you want to apply or revert to.
See Package Manager Console Parameters for additional parameters options.
How to use the Update-Database command with .NET CLI in EF Core?
To use the dotnet ef database update
command with the .NET Command Line Interface (.NET CLI) in EF Core:
- Open a command prompt or terminal.
- Navigate to the directory containing your context.
- Run the following command:
dotnet ef database update
For targeting a specific migration or reverting to a previous migration, you can use:
dotnet ef database update [MigrationName]
NOTE: Replace [MigrationName]
with the name of the migration you want to apply or revert to.
See Command Line Parameters for additional parameters options.
What does the Update-Database command do in EF Core?
When you use the Update-Database
command with PMC or dotnet ef database update
with .NET CLI, both utilize EF Core Tools to perform several tasks:
- First, EF Core Tools fetch all migrations that have been applied to your database.
- Then, EF Core Tools determine whether a migration should be applied or reverted.
- If the database needs to be updated to a more recent migration, the
Up Method
for each migration since the last one applied will be run. - If the database needs to be reverted to a previous migration, the
Down Method
for each migration up to the specified migration will be run.
- If the database needs to be updated to a more recent migration, the
What are the parameters for the Update Database command in EF Core?
Package Manager Console (PMC)
The Update-Database
command with PMC provides several optional options:
Option | Description |
---|---|
-Name | The name of the migration you wish to apply. If not specified, the most recent migration will be used. For example, Update-Database AddingEFExtensions will apply all unapplied migrations up to the one named AddingEFExtensions. In the case of a rollback, Update-Database AddingEFExtensions will revert all applied migration down to the one named AddingEFExtensions. |
-Context | The context class to use that contains the migration you want to apply, it can be either the name or the fullname including namespaces. For example, Update-Database -Context Z.MyContextName will apply the migration for the Z.MyContextName context. |
-Project | The project containing the context used to apply the migration file. If omitted, the default project in the Package Manager Console is used. For example, Update-Database -Project Z.MyProjectName will apply the migration for the context in the Z.MyProjectName project. |
-StartupProject | The project with the configurations and settings. If omitted, the startup project of your solution is used. For example, Update-Database -StartupProject Z.MyStartupProjectName will apply the migration using the configurations and settings of the Z.MyStartupProjectName project. |
Update-Database AddingEFExtensions -Context Z.MyContextName -Project Z.MyProjectName -StartupProject Z.MyStartupProjectName
NOTE: The -Project
option specifies which project contains the context, while the -StartupProject
option indicates the project that has the application's configurations and settings.
.NET Command Line Interface (.NET CLI)
For those who work outside of Visual Studio or prefer the command line, EF Core Tools offer similar features for .NET CLI, but with slightly different syntax:
Option | Short | Description |
---|---|---|
[TargetMigration] | The name of the migration to apply. If omitted, all pending migrations will be applied. | |
--context | -c | The context class to use when applying the migration. |
--project | -p | The project that contains the context used to apply the migration. |
--startup-project | -s | The project with the necessary configurations and settings to apply the migrations. |
dotnet ef database update [TargetMigration] -c MyContext -p MigrationsProject -s StartupProject
NOTE: Refer to the documentation above about Package Manager Console Parameters for additional examples.