Skip to content

Asp.Net Core EF Migration Commands

Entity Framework Core is an ORM used with .Net core, and very popular in developers using .net stack. Migrations are very useful as they give developers the option to manage database changes and track them easily. Here I am listing important EF core migration-related commands.

Add Migration

To add a migration, use below command:

dotnet ef add <migrationname>

In case, you are using multiple database context in your application then you can specify the database context against which you are creating the migration.

dotnet ef add <migrationname> --context <contextname>

For example:

dotnet ef add Initial --context DemoAPI.Data.FirstDatabaseDbContext

You can also specify the folder where migration related files will be generated. You can use -o flag and give the related path from your root application. So for the above example:

dotnet ef add Initial --context DemoAPI.Data.FirstDatabaseDbContext -o Migrations/FirstDabase

This will create the above folder path, if not already exist, then add the migration files in that folder.

Update Database

To update database, you can use below command.

dotnet ef update database

If you are using multiple database context, then you will need to specify the context,

dotnet ef update database --context <contextname>

For example:

dotnet ef update database --context DemoAPI.Data.FirstDatabaseDbContext

Thanks.

Be First to Comment

Leave a Reply

Your email address will not be published.