Skip to content

Handle seed-data cycle diagnostics without crashing during migration scaffolding - #38589

Merged
AndriySvyryd merged 4 commits into
mainfrom
copilot/fix-sequence-contains-no-elements-error
Jul 13, 2026
Merged

AndriySvyryd merged 4 commits into
mainfrom
copilot/fix-sequence-contains-no-elements-error

Conversation

Copilot AI commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #36682

Scaffolding migrations could throw InvalidOperationException: Sequence contains no elements when parent/child seed data introduced a cycle in the data dependency graph. Instead of producing a usable circular-dependency error, cycle formatting itself crashed.

  • Cycle formatting hardening (CommandBatchPreparer)

    • Made cycle-edge inspection resilient when a cycle segment has no edge metadata.
    • Avoided First()-on-empty behavior in cycle message construction so diagnostics can proceed.
  • Entryless command formatting support

    • Updated command formatter to handle modification commands with no tracked entries by formatting from store metadata (schema.table + state).
    • Added debug assertions documenting invariants for entryless commands and cycle formatting assumptions.
  • Regression coverage (MigrationsModelDifferTest)

    • Added a focused model-diff test reproducing a WorkflowType/WorkflowStep seed-data cycle.
    • Asserts the failure is reported as circular dependency and no longer as Sequence contains no elements.
// Before: could throw from cycle formatting
// switch (edges.First().Metadata)

// After: tolerate missing edge metadata in cycle diagnostics
var firstEdge = edges.FirstOrDefault();
switch (firstEdge?.Metadata)
{
    // existing metadata-specific formatting...
    default:
        builder.AppendLine(" <-");
        break;
}

Copilot AI changed the title [WIP] Fix sequence contains no elements error in migration Handle seed-data cycle diagnostics without crashing during migration scaffolding Jul 9, 2026
Copilot AI requested a review from AndriySvyryd July 9, 2026 01:36
@AndriySvyryd
AndriySvyryd requested a review from Copilot July 9, 2026 17:21
@AndriySvyryd
AndriySvyryd marked this pull request as ready for review July 9, 2026 17:23
@AndriySvyryd
AndriySvyryd requested a review from a team as a code owner July 9, 2026 17:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This pull request hardens circular-dependency diagnostics produced during migration scaffolding when seed data introduces dependency cycles, ensuring cycle formatting doesn’t itself throw and obscure the real error.

Changes:

  • Made CommandBatchPreparer cycle formatting resilient to missing/empty edge sequences (FirstOrDefault() instead of First()).
  • Updated command formatting to support entryless IReadOnlyModificationCommand instances (common for seed data scaffolding).
  • Added a regression test covering a WorkflowType/WorkflowStep seed-data cycle and asserting a circular-dependency error (not Sequence contains no elements).

Reviewed changes

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

File Description
test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs Adds regression test ensuring seed-data cycles surface as circular dependency diagnostics.
src/EFCore.Relational/Update/Internal/CommandBatchPreparer.cs Hardens cycle formatting and supports formatting modification commands with no tracked entries.

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings July 9, 2026 17:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

@AndriySvyryd
AndriySvyryd requested a review from cincuranet July 10, 2026 06:56
@AndriySvyryd
AndriySvyryd merged commit 8fb7fbb into main Jul 13, 2026
17 checks passed
@AndriySvyryd
AndriySvyryd deleted the copilot/fix-sequence-contains-no-elements-error branch July 13, 2026 17:34
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 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.

Sequence contains no elements when adding parent and child entity data in the same migration

4 participants