What is the equivalent of the -IgnoreChanges switch for entity-framework core in CLI?

Viewed 11802

With Visual Studio one can run the command

Add-Migration InitialCreate -IgnoreChanges 

in the Package Manage Console when creating the first migration of the model of an existing database with Code First workflow.

What is the equivalent for CLI? The add command would be like this:

dotnet ef migrations add InitialCreate

but what about the ignore switch?

3 Answers

In the absence of any other way, one can empty the Up and Down method blocks of the migration of all code and run database update.

public partial class Initial : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {

    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
    }
}

My project has an existing database and we wanted to use migrations, since we do not have IgnoreChanges in EFCore what i did was ran the command

Add-Migration -Name InitialMigration in the package manager console of visual studio, this created the InitialMigration.cs file for me

And then I commented out the code in the Up function in the InitialMigration.cs and ran the command update-database in the package manager console.

Doing this creates a table named dbo.__EFMigrationsHistory in your database which keeps a track of your migrations.

Later when you add or remove the column, just run the Add-Migration command which will create a new migration for you make sure you validate the Up and Down function both to make sure everything looks ok and is as expected.

Once you validate, run the update-database function which should run the new migration script for you.

For now this is how we went ahead with our implementation.

The ability to exclude tables from migrations was introduced in EF Core 5.0. It can be performed in this way :

1. You can setup your entity configuration migrations to not create undesired tables.

protected override void OnModelCreating(ModelBuilder modelBuilder
{
     modelBuilder.Entity<IdentityUser>()
        .ToTable("AspNetUsers", t => t.ExcludeFromMigrations());
}
  1. Execute ef migrations add [NAME] where [NAME] refers to the migration name. With this configuration migrations above it will not create the AspNetUsers table, but IdentityUser is still included in the model and can be used normally.

  2. If you need to start managing the table using migrations again then a new migration should be created where AspNetUsers is not excluded. The next migration will now contain any changes made to the table.

From EF Core | Microsoft Docs

Related