Skip to content

The first migration after the move from 5.x cannot be scaffolded when it changes anything: EF Core diffs Down with the snapshot as the target #98

Closed

Description

@CaffeinatedCoder

Found by the pre-preview sweep of main @ 948496a (lens: new check against the standing rules); reproduced in a scratch test.

What happens

A project moves from 5.x, declares the same exclusion constraints with DeclarePostgres, and its first migrations add also holds one change of its own (a new column). The command fails:

System.InvalidOperationException: Booking carries the annotation 'CustomExclusion:Constraints' in the model the
migration leads to. It is the format of an earlier package, which this version reads from a model snapshot only: …
Remove the annotation and declare the object.
   at SchemaOperationPlanner.Resolve(IRelationalModel model, Boolean isTarget)   SchemaOperationPlanner.cs:355

The annotation is in the snapshot, not in the model, so the advice cannot be followed. migrations remove of the last 5.x migration fails the same way.

Why

The planner takes the second argument of a diff for "the model the migration leads to" (SchemaOperationPlanner.cs:86-87, isTarget). EF Core does not call it that way only (MigrationsScaffolder, EF Core Design 11.0.0-rc.3, decompiled):

  • ScaffoldMigration computes Up as GetDifferences(snapshot, model) and, when Up is not empty, Down as GetDifferences(model, snapshot): the snapshot is the second argument.
  • RemoveMigration calls HasDifferences(lastMigration.TargetModel, snapshot): two snapshots.

So every rule written for the target also runs on a snapshot: the refusal of a 5.x annotation (:354), the derived-type refusal of #82 (:337) and the name claims of #96 (:94). "A snapshot that holds either stays diffable" holds in the Up direction only. The handover tests (LegacyHandoverTests) diff to zero operations, and then EF Core never computes Down, which is why they did not see it. Only the 5.x annotation was run; the other two rules are traced.

Scenario

public sealed class DeclaredWithANewColumn : ITestModel
{
    public static void Build(ModelBuilder modelBuilder)
    {
        MovedModels.Declared.Build(modelBuilder);
        modelBuilder.Entity<Slot>().Property<string>("Note");
    }
}

[TestMethod]
public void First_migration_after_the_move_with_one_more_change_scaffolds()
{
    using var context = LegacyFixture.Context<DeclaredWithANewColumn>();
    var differ = context.GetService<IMigrationsModelDiffer>();
    var snapshot = LegacyFixture.Snapshot(context);

    Assert.AreEqual(1, differ.GetDifferences(snapshot, context.Relational()).Count);   // passes
    Assert.AreEqual(1, differ.GetDifferences(context.Relational(), snapshot).Count);   // throws
}

[TestMethod]
public void Remove_of_the_last_5x_migration_can_ask_HasDifferences()
{
    using var context = LegacyFixture.Context<MovedModels.Declared>();
    var snapshot = LegacyFixture.Snapshot(context);
    Assert.IsFalse(context.GetService<IMigrationsModelDiffer>().HasDifferences(snapshot, snapshot));   // throws
}

To decide

What tells the consumer's model from a snapshot. The position in the diff does not.

  1. The context's own design-time model, by reference. The rules that hold a consumer's model run on that instance wherever it stands; every other model is only read, 5.x annotations included. A 5.x annotation in the consumer's own model stays an error.
  2. Read a 5.x annotation on either side and refuse it only beside a declaration of the same object. This loses the error for an annotation left in the model.

Test

The two above, in test/CodoMetis.EFCore.SchemaKit.PostgreSQL.Tests/Legacy, and the same shape for the derived-type refusal and for a name collision in a snapshot. The real path is the consumer smoke test (#34): a 5.x snapshot, a declared model with one more change, migrations add, then migrations remove.

Activity

  1. added this to the 11.0.0 milestone on Nov 14, 2026
  2. added
    bug
    severity/high
    package/core
    package/postgresql
    area/migrations
    on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/migrationsbugpackage/corepackage/postgresqlseverity/high

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions