Skip to content

Removing migration adds parameter to all ToTable() methods, but then appears to trash entire migration #37510

Closed as duplicate

Description

@SoftCircuits

Bug description

I'm seeing a strange issue where I'm getting many migration changes when there are no changes to my code. I have not made any manual updates to Visual Studio or my solution.

I start with the following command.

add-migration AddPurchaseOrderType

This produces the following migration file:

/// <inheritdoc />
public partial class AddPurchaseOrderType : Migration
{
    /// <inheritdoc />
    protected override void Up(MigrationBuilder migrationBuilder)
    {

    }

    /// <inheritdoc />
    protected override void Down(MigrationBuilder migrationBuilder)
    {

    }
}

Then use the following command:

remove-migration

This undoes the migration but then I see 92 changes were added to ApplicationDbContextModelSnapshot.cs! All of these changes have to do with add a second parameter to the ToTable() method in the form b.ToTable("Attachments", (string)null);.

Okay, should be okay right? But now when I run the following command:

add-migration AddPurchaseOrderType

This produces the following migration file. It almost appears as if it is recreating my entire schema. Is this a known issue? Can anyone help with this?

/// <inheritdoc />
public partial class AddPurchaseOrderType : Migration
{
    /// <inheritdoc />
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropForeignKey(
            name: "FK_PurchaseOrders_Products_ProductId",
            table: "PurchaseOrders");

        migrationBuilder.DropForeignKey(
            name: "FK_Railcars_Products_ProductId",
            table: "Railcars");

        migrationBuilder.DropForeignKey(
            name: "FK_ShipProducts_Products_ProductId",
            table: "ShipProducts");

        migrationBuilder.DropForeignKey(
            name: "FK_Transfers_Railcars_FromRailcarId",
            table: "Transfers");

        migrationBuilder.DropForeignKey(
            name: "FK_Transfers_Railcars_ToRailcarId",
            table: "Transfers");

        migrationBuilder.DropForeignKey(
            name: "FK_Transfers_Trucks_FromTruckId",
            table: "Transfers");

        migrationBuilder.DropForeignKey(
            name: "FK_Transfers_Trucks_ToTruckId",
            table: "Transfers");

        migrationBuilder.DropCheckConstraint(
            name: "ValidateFromForeignKeys",
            table: "Transfers");

        migrationBuilder.DropCheckConstraint(
            name: "ValidateToForeignKeys",
            table: "Transfers");

        migrationBuilder.DropIndex(
            name: "IX_ShipProducts_ShipId",
            table: "ShipProducts");

        migrationBuilder.DropIndex(
            name: "IX_Railcars_ProductId",
            table: "Railcars");

        migrationBuilder.DropIndex(
            name: "IX_PurchaseOrders_ProductId",
            table: "PurchaseOrders");

        migrationBuilder.DropColumn(
            name: "InboundQuantity",
            table: "Railcars");

        migrationBuilder.DropColumn(
            name: "ProductId",
            table: "Railcars");

        migrationBuilder.DropColumn(
            name: "ProductId",
            table: "PurchaseOrders");

        migrationBuilder.RenameColumn(
            name: "ToTruckId",
            table: "Transfers",
            newName: "ToTruckProductId");

        migrationBuilder.RenameColumn(
            name: "ToRailcarId",
            table: "Transfers",
            newName: "ToRailcarProductId");

        migrationBuilder.RenameColumn(
            name: "FromTruckId",
            table: "Transfers",
            newName: "FromTruckProductId");

        migrationBuilder.RenameColumn(
            name: "FromRailcarId",
            table: "Transfers",
            newName: "FromRailcarProductId");

        migrationBuilder.RenameIndex(
            name: "IX_Transfers_ToTruckId",
            table: "Transfers",
            newName: "IX_Transfers_ToTruckProductId");

        migrationBuilder.RenameIndex(
            name: "IX_Transfers_ToRailcarId",
            table: "Transfers",
            newName: "IX_Transfers_ToRailcarProductId");

        migrationBuilder.RenameIndex(
            name: "IX_Transfers_FromTruckId",
            table: "Transfers",
            newName: "IX_Transfers_FromTruckProductId");

        migrationBuilder.RenameIndex(
            name: "IX_Transfers_FromRailcarId",
            table: "Transfers",
            newName: "IX_Transfers_FromRailcarProductId");

        migrationBuilder.CreateTable(
            name: "PurchaseOrderProducts",
            columns: table => new
            {
                PurchaseOrderId = table.Column<int>(type: "int", nullable: false),
                ProductId = table.Column<int>(type: "int", nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_PurchaseOrderProducts", x => new { x.PurchaseOrderId, x.ProductId });
                table.ForeignKey(
                    name: "FK_PurchaseOrderProducts_Products_ProductId",
                    column: x => x.ProductId,
                    principalTable: "Products",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
                table.ForeignKey(
                    name: "FK_PurchaseOrderProducts_PurchaseOrders_PurchaseOrderId",
                    column: x => x.PurchaseOrderId,
                    principalTable: "PurchaseOrders",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
            });

        migrationBuilder.CreateTable(
            name: "RailcarProducts",
            columns: table => new
            {
                Id = table.Column<int>(type: "int", nullable: false)
                    .Annotation("SqlServer:Identity", "1, 1"),
                RailcarId = table.Column<int>(type: "int", nullable: false),
                ProductId = table.Column<int>(type: "int", nullable: false),
                InboundQuantity = table.Column<double>(type: "float", nullable: false, defaultValue: 0.0)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_RailcarProducts", x => x.Id);
                table.ForeignKey(
                    name: "FK_RailcarProducts_Products_ProductId",
                    column: x => x.ProductId,
                    principalTable: "Products",
                    principalColumn: "Id");
                table.ForeignKey(
                    name: "FK_RailcarProducts_Railcars_RailcarId",
                    column: x => x.RailcarId,
                    principalTable: "Railcars",
                    principalColumn: "Id");
            });

        migrationBuilder.CreateTable(
            name: "TruckProducts",
            columns: table => new
            {
                Id = table.Column<int>(type: "int", nullable: false)
                    .Annotation("SqlServer:Identity", "1, 1"),
                TruckId = table.Column<int>(type: "int", nullable: false),
                ProductId = table.Column<int>(type: "int", nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_TruckProducts", x => x.Id);
                table.ForeignKey(
                    name: "FK_TruckProducts_Products_ProductId",
                    column: x => x.ProductId,
                    principalTable: "Products",
                    principalColumn: "Id");
                table.ForeignKey(
                    name: "FK_TruckProducts_Trucks_TruckId",
                    column: x => x.TruckId,
                    principalTable: "Trucks",
                    principalColumn: "Id");
            });

        migrationBuilder.AddCheckConstraint(
            name: "ExactlyOneDestination",
            table: "Transfers",
            sql: "(CASE WHEN [ToTruckProductId] IS NOT NULL THEN 1 ELSE 0 END +\r\nCASE WHEN [ToRailcarProductId] IS NOT NULL THEN 1 ELSE 0 END +\r\nCASE WHEN [ToShipProductId] IS NOT NULL THEN 1 ELSE 0 END +\r\nCASE WHEN [ToStorageId] IS NOT NULL THEN 1 ELSE 0 END) = 1");

        migrationBuilder.AddCheckConstraint(
            name: "ExactlyOneSource",
            table: "Transfers",
            sql: "(CASE WHEN [FromTruckProductId] IS NOT NULL THEN 1 ELSE 0 END +\r\nCASE WHEN [FromRailcarProductId] IS NOT NULL THEN 1 ELSE 0 END +\r\nCASE WHEN [FromShipProductId] IS NOT NULL THEN 1 ELSE 0 END +\r\nCASE WHEN [FromStorageId] IS NOT NULL THEN 1 ELSE 0 END) = 1");

        migrationBuilder.CreateIndex(
            name: "IX_ShipProducts_ShipId_ProductId",
            table: "ShipProducts",
            columns: new[] { "ShipId", "ProductId" },
            unique: true);

        migrationBuilder.CreateIndex(
            name: "IX_PurchaseOrderProducts_ProductId",
            table: "PurchaseOrderProducts",
            column: "ProductId");

        migrationBuilder.CreateIndex(
            name: "IX_RailcarProducts_ProductId",
            table: "RailcarProducts",
            column: "ProductId");

        migrationBuilder.CreateIndex(
            name: "IX_RailcarProducts_RailcarId_ProductId",
            table: "RailcarProducts",
            columns: new[] { "RailcarId", "ProductId" },
            unique: true);

        migrationBuilder.CreateIndex(
            name: "IX_TruckProducts_ProductId",
            table: "TruckProducts",
            column: "ProductId");

        migrationBuilder.CreateIndex(
            name: "IX_TruckProducts_TruckId_ProductId",
            table: "TruckProducts",
            columns: new[] { "TruckId", "ProductId" },
            unique: true);

        migrationBuilder.AddForeignKey(
            name: "FK_ShipProducts_Products_ProductId",
            table: "ShipProducts",
            column: "ProductId",
            principalTable: "Products",
            principalColumn: "Id");

        migrationBuilder.AddForeignKey(
            name: "FK_Transfers_RailcarProducts_FromRailcarProductId",
            table: "Transfers",
            column: "FromRailcarProductId",
            principalTable: "RailcarProducts",
            principalColumn: "Id");

        migrationBuilder.AddForeignKey(
            name: "FK_Transfers_RailcarProducts_ToRailcarProductId",
            table: "Transfers",
            column: "ToRailcarProductId",
            principalTable: "RailcarProducts",
            principalColumn: "Id");

        migrationBuilder.AddForeignKey(
            name: "FK_Transfers_TruckProducts_FromTruckProductId",
            table: "Transfers",
            column: "FromTruckProductId",
            principalTable: "TruckProducts",
            principalColumn: "Id");

        migrationBuilder.AddForeignKey(
            name: "FK_Transfers_TruckProducts_ToTruckProductId",
            table: "Transfers",
            column: "ToTruckProductId",
            principalTable: "TruckProducts",
            principalColumn: "Id");
    }

    /// <inheritdoc />
    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropForeignKey(
            name: "FK_ShipProducts_Products_ProductId",
            table: "ShipProducts");

        migrationBuilder.DropForeignKey(
            name: "FK_Transfers_RailcarProducts_FromRailcarProductId",
            table: "Transfers");

        migrationBuilder.DropForeignKey(
            name: "FK_Transfers_RailcarProducts_ToRailcarProductId",
            table: "Transfers");

        migrationBuilder.DropForeignKey(
            name: "FK_Transfers_TruckProducts_FromTruckProductId",
            table: "Transfers");

        migrationBuilder.DropForeignKey(
            name: "FK_Transfers_TruckProducts_ToTruckProductId",
            table: "Transfers");

        migrationBuilder.DropTable(
            name: "PurchaseOrderProducts");

        migrationBuilder.DropTable(
            name: "RailcarProducts");

        migrationBuilder.DropTable(
            name: "TruckProducts");

        migrationBuilder.DropCheckConstraint(
            name: "ExactlyOneDestination",
            table: "Transfers");

        migrationBuilder.DropCheckConstraint(
            name: "ExactlyOneSource",
            table: "Transfers");

        migrationBuilder.DropIndex(
            name: "IX_ShipProducts_ShipId_ProductId",
            table: "ShipProducts");

        migrationBuilder.RenameColumn(
            name: "ToTruckProductId",
            table: "Transfers",
            newName: "ToTruckId");

        migrationBuilder.RenameColumn(
            name: "ToRailcarProductId",
            table: "Transfers",
            newName: "ToRailcarId");

        migrationBuilder.RenameColumn(
            name: "FromTruckProductId",
            table: "Transfers",
            newName: "FromTruckId");

        migrationBuilder.RenameColumn(
            name: "FromRailcarProductId",
            table: "Transfers",
            newName: "FromRailcarId");

        migrationBuilder.RenameIndex(
            name: "IX_Transfers_ToTruckProductId",
            table: "Transfers",
            newName: "IX_Transfers_ToTruckId");

        migrationBuilder.RenameIndex(
            name: "IX_Transfers_ToRailcarProductId",
            table: "Transfers",
            newName: "IX_Transfers_ToRailcarId");

        migrationBuilder.RenameIndex(
            name: "IX_Transfers_FromTruckProductId",
            table: "Transfers",
            newName: "IX_Transfers_FromTruckId");

        migrationBuilder.RenameIndex(
            name: "IX_Transfers_FromRailcarProductId",
            table: "Transfers",
            newName: "IX_Transfers_FromRailcarId");

        migrationBuilder.AddColumn<double>(
            name: "InboundQuantity",
            table: "Railcars",
            type: "float",
            nullable: false,
            defaultValue: 0.0);

        migrationBuilder.AddColumn<int>(
            name: "ProductId",
            table: "Railcars",
            type: "int",
            nullable: true);

        migrationBuilder.AddColumn<int>(
            name: "ProductId",
            table: "PurchaseOrders",
            type: "int",
            nullable: false,
            defaultValue: 0);

        migrationBuilder.AddCheckConstraint(
            name: "ValidateFromForeignKeys",
            table: "Transfers",
            sql: "(CASE WHEN [FromRailcarId] IS NULL THEN 0 ELSE 1 END\r\n                    + CASE WHEN [FromTruckId] IS NULL THEN 0 ELSE 1 END\r\n                    + CASE WHEN [FromStorageId] IS NULL THEN 0 ELSE 1 END\r\n                    + CASE WHEN [FromShipProductId] IS NULL THEN 0 ELSE 1 END) = 1");

        migrationBuilder.AddCheckConstraint(
            name: "ValidateToForeignKeys",
            table: "Transfers",
            sql: "(CASE WHEN [ToRailcarId] IS NULL THEN 0 ELSE 1 END\r\n                    + CASE WHEN [ToTruckId] IS NULL THEN 0 ELSE 1 END\r\n                    + CASE WHEN [ToStorageId] IS NULL THEN 0 ELSE 1 END\r\n                    + CASE WHEN [ToShipProductId] IS NULL THEN 0 ELSE 1 END) = 1");

        migrationBuilder.CreateIndex(
            name: "IX_ShipProducts_ShipId",
            table: "ShipProducts",
            column: "ShipId");

        migrationBuilder.CreateIndex(
            name: "IX_Railcars_ProductId",
            table: "Railcars",
            column: "ProductId");

        migrationBuilder.CreateIndex(
            name: "IX_PurchaseOrders_ProductId",
            table: "PurchaseOrders",
            column: "ProductId");

        migrationBuilder.AddForeignKey(
            name: "FK_PurchaseOrders_Products_ProductId",
            table: "PurchaseOrders",
            column: "ProductId",
            principalTable: "Products",
            principalColumn: "Id");

        migrationBuilder.AddForeignKey(
            name: "FK_Railcars_Products_ProductId",
            table: "Railcars",
            column: "ProductId",
            principalTable: "Products",
            principalColumn: "Id");

        migrationBuilder.AddForeignKey(
            name: "FK_ShipProducts_Products_ProductId",
            table: "ShipProducts",
            column: "ProductId",
            principalTable: "Products",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);

        migrationBuilder.AddForeignKey(
            name: "FK_Transfers_Railcars_FromRailcarId",
            table: "Transfers",
            column: "FromRailcarId",
            principalTable: "Railcars",
            principalColumn: "Id");

        migrationBuilder.AddForeignKey(
            name: "FK_Transfers_Railcars_ToRailcarId",
            table: "Transfers",
            column: "ToRailcarId",
            principalTable: "Railcars",
            principalColumn: "Id");

        migrationBuilder.AddForeignKey(
            name: "FK_Transfers_Trucks_FromTruckId",
            table: "Transfers",
            column: "FromTruckId",
            principalTable: "Trucks",
            principalColumn: "Id");

        migrationBuilder.AddForeignKey(
            name: "FK_Transfers_Trucks_ToTruckId",
            table: "Transfers",
            column: "ToTruckId",
            principalTable: "Trucks",
            principalColumn: "Id");
    }
}

Your code

Please see above

Stack traces


Verbose output


EF Core version

9.0.8

Database provider

Microsoft.EntityFrameworkCore.SqlServer

Target framework

.NET 9.0

Operating system

Windows 11

IDE

Visual Studio 2022 17.14.22

Activity

  1. changed the title [-]Removing migration adds parameter to methods ToTable(), but then appears to trash entire migration[/-] [+]Removing migration adds parameter to all ToTable() methods, but then appears to trash entire migration[/+] on Jan 14, 2026
  2. MihuBot commented on Jan 14, 2026

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  3. AndriySvyryd commented on Jan 14, 2026

    @AndriySvyryd
    Member

    Please share a small project that shows the issue with most of the database schema being recreated, otherwise we'll treat this as a duplicate of #26067

  4. SoftCircuits commented on Jan 14, 2026

    @SoftCircuits
    Author

    @AndriySvyryd How is it a duplicate? The other post didn't seem to be having any issue with migrations. They only seemed to be asking about the null argument being added to ToTable(). In my case, i'm completely blocked!

    And how can I duplicate this? What information would do that? I've been working on this project for a few years and never seen this problem until now. How can I recreate it and expect it to have the same issue?

    There is a real bug here that has me completely blocked. Simply closing it as a duplicate leaves me high and dry. I'd think you'd be more interested in a bug like this! Not even a hint as to what the software is doing here.

  5. AndriySvyryd commented on Jan 15, 2026

    @AndriySvyryd
    Member

    @AndriySvyryd How is it a duplicate? The other post didn't seem to be having any issue with migrations. They only seemed to be asking about the null argument being added to ToTable(). In my case, i'm completely blocked!

    According to your description #26067 is the root issue that causes an invalid migration after one is removed. You can revert the changes made by remove-migration in your SCCS to get to a good state.

    And how can I duplicate this? What information would do that? I've been working on this project for a few years and never seen this problem until now. How can I recreate it and expect it to have the same issue?

    1. Remove something that was part of the model from your project (entity type, property, etc.)
    2. Create a new migration
    3. Create a commit
    4. Create a new (empty) migration
    5. Revert the empty migration
    6. Create a new migration
      1. If it is still applying the wrong operations go to 1
      2. Otherwise, revert to the previous commit, go to 1 and choose a different object. If there are no more parts of the model or project that you haven't tried removing the project will contain the minimal repro and can be shared.
  6. SoftCircuits commented on Jan 16, 2026

    @SoftCircuits
    Author

    @AndriySvyryd I really appreciate this but those instructions were not clear to me.

    Note that I have already reverted everything. And I've since added a non-empty migration. But if I remove the migration, it gets corrupt again.

    1. I assume you are talking about a GIT commit here? (Does this issue have anything to do with GIT?)
    2. ii. If it no longer applies the wrong operation, why am I going back to 1 and choosing a different object? There are always more parts to remove until everything is gone. Why would I remove everything?

    I'm really sorry but has anyone written about what is happening exactly? I'm still at a loss why this is happening or exactly what I'm trying to fix.

  7. AndriySvyryd commented on Jan 17, 2026

    @AndriySvyryd
    Member

    I assume you are talking about a GIT commit here? (Does this issue have anything to do with GIT?)

    Yes, if you use GIT. It's not related, but using it would help to distill the minimal repro

    ii. If it no longer applies the wrong operation, why am I going back to 1 and choosing a different object? There are always more parts to remove until everything is gone. Why would I remove everything?

    The point is to remove everything that's not producing the wrong operation.

  8. SoftCircuits commented on Jan 19, 2026

    @SoftCircuits
    Author

    @AndriySvyryd I'm sorry to continue this, but I had removed everything that wasn't producing a wrong operation. I reverted ApplicationDbContextModelSnapshot.cs, and I got everything back to normal. I even created a new migration, and it seemed to work fine.

    But today I added an additional migration and saw I made an error. So I removed the migration and it screwed up my snapshot all over again. I used to be able to remove migrations. Is there anything I can read about why the problem exists
    and how to get back to how it was? I can correct everything, but if I remove a migration, it messes up my snapshot and then it wants to recreate my entire schema.

  9. AndriySvyryd commented on Jan 19, 2026

    @AndriySvyryd
    Member

    I would need to investigate the issue first, for that I need a small repro project that could be created by following the steps mentioned above

  10. SoftCircuits commented on Jan 21, 2026

    @SoftCircuits
    Author

    @AndriySvyryd I appreciate your help. I may put that together, but it would be a huge effort as it's a big solution with many projects.

    More importantly, after adding several migrations, the behavior seems to have changed. Now, when I add an empty migration and then remove it, it alters the snapshot as before. But then if I add a new migration, it clears those changes to the snapshot and reverts it to what it was before. So it may be good now. We'll see.

    I still don't understand any of this, and #26067 doesn't explain it. My understanding is that the changes to the snapshot are meant to not change the meaning of the schema in any way. But for me, it was. I wish there was a write up about this somewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions