dotnet Verified current stable Not installed? System Operations

Dotnet / Add New Migration

Add New Migration

Adds a new migration to the Entity Framework in a .NET environment.

$
Terminal
dotnet ef migrations add <name>

When To Use

When database schema changes necessitate a migration process for version control.

Pro Tip

Follow up with `dotnet dotnet ef database update` to apply the migration immediately after creation for consistency.

Command Builder

Tune the command before you copy it

Back to syntax
$
Generated Command
dotnet ef migrations add <name>

Terminal Output

Expected runtime feedback

Simulated preview
>
Output
$ dotnet ef migrations add InitialCreate

Applying migration '20231005123456_InitialCreate'.
Done.

List of migrations:
| Migration ID                     | Product Version |
| -------------------------------- | ---------------- |
| 20231005123456_InitialCreate     | 5.0.0           |

Anatomy of Output

Understanding the result

Creating migration 'MigrationName'... Migration Creation

This indicates a new migration with the provided name is being initiated.

Migration 'MigrationName' created successfully. Migration Status

Confirms the new migration has been generated.

Ensure to apply the migration using 'dotnet ef database update'. Next Steps

Advises the user to apply the migration to the database.

Power User Variants

Optimized versions

dotnet ef migrations add MigrationName --ignore-changes

Creates a migration that doesn't include any changes to the schema.

dotnet ef migrations add MigrationName --output-dir MigrationsFolder

Specifies a custom output directory for the migration files.

Troubleshooting

Common pitfalls

Error: Migration name cannot be empty.

Solution: Ensure that a valid migration name is provided.

Cannot add migration: Model state is invalid.

Solution: Check entity configurations and relationships for correctness.

Migration failed due to an untracked schema change.

Solution: Ensure all changes are properly defined in the DbContext before creating a migration.

Command Breakdown

What each part is doing

dotnet
Base Command
The executable that performs this operation. Here it runs Dotnet before the shell applies any redirect operators.
<name>
name
The value supplied for name.

How To Run

Execution path

  1. Step 1

    Open terminal in your project directory.

  2. Step 2

    Run the command: `dotnet ef migrations add InitialCreate`.

  3. Step 3

    Check the migrations with `dotnet ef migrations list`.

Alternative Approaches

Comparable commands in other tools

Alternative system operations tools for the same job.