Repository navigation
Description
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 aboveStack 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
[-]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[/+]MihuBot commentedon Jan 14, 2026
I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):
(string)nullargument to all ToTable calls in the snapshot #26067AndriySvyryd commentedon Jan 14, 2026
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
(string)nullargument to all ToTable calls in the snapshot #26067SoftCircuits commentedon Jan 14, 2026
@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.
AndriySvyryd commentedon Jan 15, 2026
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-migrationin your SCCS to get to a good state.SoftCircuits commentedon Jan 16, 2026
@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.
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.
AndriySvyryd commentedon Jan 17, 2026
Yes, if you use GIT. It's not related, but using it would help to distill the minimal repro
The point is to remove everything that's not producing the wrong operation.
SoftCircuits commentedon Jan 19, 2026
@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.
AndriySvyryd commentedon Jan 19, 2026
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
SoftCircuits commentedon Jan 21, 2026
@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.