Skip to content

Record latest migration ID in model snapshot as property to detect diverged migration trees#37689

Open
Copilot wants to merge 8 commits intomainfrom
copilot/record-latest-migration-id
Open

Record latest migration ID in model snapshot as property to detect diverged migration trees#37689
Copilot wants to merge 8 commits intomainfrom
copilot/record-latest-migration-id

Conversation

Copy link
Contributor

Copilot AI commented Feb 13, 2026

  • Update IMigrationsCodeGenerator.GenerateSnapshot to accept latest migration ID parameter
  • Update MigrationsCodeGenerator.GenerateSnapshot base class to accept latest migration ID
  • Update CSharpMigrationsGenerator.GenerateSnapshot to generate property with latest migration ID
  • Update MigrationsScaffolder.ScaffoldMigration to pass latest migration ID
  • Update MigrationsScaffolder.RemoveMigration to pass latest migration ID
  • Add LatestMigrationId property to ModelSnapshot base class
  • Add tests to validate the migration ID appears as a property in the snapshot
  • Run all migration generator tests (154 tests pass)
  • Address code review feedback: Changed from model annotation to a property on the ModelSnapshot class
  • Address code review feedback: Updated documentation link to https://aka.ms/efcore-docs-migrations-conflicts
  • Fix failing MigrationTests in SqlServer.HierarchyId.Tests to include expected LatestMigrationId property

Changes from Review Feedback

Changed the implementation from a model annotation to a property on the ModelSnapshot class:

  • Before: modelBuilder.HasAnnotation("LatestMigrationId", "20240213120000_Migration");
  • After: public override string LatestMigrationId => "20240213120000_Migration";

Updated all documentation links to point to https://aka.ms/efcore-docs-migrations-conflicts for consistency.

Fixed failing tests by updating expected snapshot code to include the new LatestMigrationId property that is now generated for all migrations.

This makes the migration ID directly accessible as a property on the snapshot instance while still causing merge conflicts when branches diverge.

Original prompt

This section details on the original issue you should resolve

<issue_title>Record the latest migration id in the model snapshot</issue_title>
<issue_description>With this merging a branch with a diverged migration tree will cause a source control conflict, as both main and the branch attempt to modify the same thing. This will alert the user to discard one the migration trees and create a new, unified migration.</issue_description>

<agent_instructions>Make sure to also add a comment in the model snapshot explaining what the user should do in case of a merge conflict for this line.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits February 13, 2026 00:56
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title [WIP] Record the latest migration id in the model snapshot Record latest migration ID in model snapshot to detect diverged migration trees Feb 13, 2026
Copilot AI requested a review from AndriySvyryd February 13, 2026 01:10
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title Record latest migration ID in model snapshot to detect diverged migration trees Record latest migration ID in model snapshot as annotation to detect diverged migration trees Feb 13, 2026
Copilot AI requested a review from AndriySvyryd February 13, 2026 01:54
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title Record latest migration ID in model snapshot as annotation to detect diverged migration trees Record latest migration ID in model snapshot as property to detect diverged migration trees Feb 13, 2026
Copilot AI requested a review from AndriySvyryd February 13, 2026 02:10
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI requested a review from AndriySvyryd February 13, 2026 02:30
@AndriySvyryd AndriySvyryd marked this pull request as ready for review February 13, 2026 02:55
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner February 13, 2026 02:55
Copilot AI review requested due to automatic review settings February 13, 2026 02:55
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements a mechanism to detect diverged migration trees during source control merges by recording the latest migration ID as a property in model snapshots. When branches have different migration histories, the merge will create a conflict on this property, alerting developers to the divergence.

Changes:

  • Added LatestMigrationId virtual property to ModelSnapshot base class that returns null by default
  • Updated migrations code generator interfaces and implementations to accept an optional latestMigrationId parameter
  • Modified snapshot generation to emit the LatestMigrationId property override with helpful comments explaining merge conflict resolution
  • Updated MigrationsScaffolder to pass the current migration ID when creating snapshots and the previous migration ID when removing migrations
  • Added comprehensive tests validating both the presence and absence of migration IDs in generated snapshots

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/EFCore.Relational/Infrastructure/ModelSnapshot.cs Added virtual LatestMigrationId property returning null by default, with XML documentation pointing to migration conflicts documentation
src/EFCore.Design/Migrations/Design/IMigrationsCodeGenerator.cs Added optional latestMigrationId parameter to GenerateSnapshot interface method
src/EFCore.Design/Migrations/Design/MigrationsCodeGenerator.cs Added optional latestMigrationId parameter to abstract GenerateSnapshot base method
src/EFCore.Design/Migrations/Design/CSharpMigrationsGenerator.cs Implemented code generation for LatestMigrationId property override with merge conflict guidance comments
src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs Updated ScaffoldMigration to pass current migration ID and RemoveMigration to pass previous migration ID to snapshot generator
test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationsGeneratorTest.ModelSnapshot.cs Added tests verifying snapshot generation with and without migration IDs, including compilation and runtime behavior

@AndriySvyryd
Copy link
Member

@copilot
Tests in Microsoft.EntityFrameworkCore.SqlServer.MigrationTests and Microsoft.EntityFrameworkCore.Design.OperationExecutorTest are failing

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 13, 2026

@copilot
Tests in Microsoft.EntityFrameworkCore.SqlServer.MigrationTests and Microsoft.EntityFrameworkCore.Design.OperationExecutorTest are failing

Fixed the failing tests. The MigrationTests were comparing against hardcoded expected snapshot code that didn't include the new LatestMigrationId property. Updated the tests to dynamically include the generated migration ID in the expected output. All tests now pass. Commit: 319b537

Copy link
Member

@roji roji left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the velocity!

@roji roji removed their assignment Feb 14, 2026
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.

Record the latest migration id in the model snapshot

3 participants