Skip to content

RevertMigration: commit both migrations, .NET 10, tests - #2265

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/104010-efcore-how-to-revert-a-migration
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/104010-efcore-how-to-revert-a-migration

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample for the article "How to Revert a Migration in EF Core" (https://code-maze.com/efcore-how-to-revert-a-migration/).

Fixes the sample, which did not run. Models/Hangar.cs and Program.cs use HasDoors, but the only committed migration was Init, which has no HasDoors column, so a clean clone applied Init and then crashed on the first SaveChanges() with "SQLite Error 1: 'table Hangars has no column named HasDoors'".

Changes, all inside dotnet-efcore/RevertMigration:

  • Init regenerated from the start state (model without HasDoors) and Hangar_HasDoors added. Both keep the migration IDs the article prints (20231209224404_Init, 20231209231131_Hangar_HasDoors). A clean clone now applies both and sits where the article's revert section begins.
  • The regenerated Init writes nullable: true for HangarNumber, TailNumber, NumberOfEngines, MaxAirSpeed, RunsOnJetFuel and HangarId, matching the nullable model; the old Init had them as nullable: false.
  • RevertMigration.csproj: net8.0 to net10.0; Microsoft.EntityFrameworkCore.Sqlite and Microsoft.EntityFrameworkCore.Design 8.0.0 to 10.0.12. DefaultItemExcludes keeps the test folder out of the console project.
  • New xunit test project RevertMigration.Tests (added to the solution): applies both migrations to a temporary SQLite file and checks the HasDoors column, saves a hangar with doors, reverts to Init and checks the column is gone, and reverts to 0. The tests point the context at the temp file with Database.SetConnectionString, so AirportDbContext is unchanged. All four tests fail against the old migrations.

Packages, re-queried on NuGet on 2026-10-04: Microsoft.EntityFrameworkCore.Sqlite 10.0.12, Microsoft.EntityFrameworkCore.Design 10.0.12, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, Microsoft.NET.Test.Sdk 18.10.1, coverlet.collector 10.1.0.

Local: dotnet build -c Release 0 warnings 0 errors; dotnet test 4 passed; dotnet list package --vulnerable --include-transitive clean; dotnet ef database update applies both migrations and dotnet run prints both "Just created" lines (SDK 10.0.302, runtime 10.0.10, dotnet-ef 10.0.12).

Note for anyone who ran an older version: the sample keeps its database at LocalApplicationData/airport.db, outside the project folder, so a stale airport.db from an earlier run survives a clean clone. Delete it before running.

The folder held the model with HasDoors but only the Init migration, so a
clean clone crashed on the first insert with "table Hangars has no column
named HasDoors". Init is regenerated from the start state and
Hangar_HasDoors is added, both keeping the migration IDs the article prints.
The regenerated Init also matches the nullable model columns.

Retarget to net10.0 with EF Core 10.0.12 and add an xunit test project that
applies, reverts and fully reverts the migrations against a temporary
SQLite file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant