Skip to content

EnsureCreated: run the UseSeeding and UseAsyncSeeding delegates (Fixes #66) - #115

Merged
ajcvickers merged 1 commit into
releasefrom
ensurecreated-seeding
Nov 14, 2026
Merged

ajcvickers merged 1 commit into
releasefrom
ensurecreated-seeding

Conversation

@ajcvickers

Copy link
Copy Markdown
Owner

EnsureCreated and EnsureCreatedAsync ignored the delegates set with UseSeeding / UseAsyncSeeding (EF9+), so data seeded that way was never written. They now run them, as the relational providers do.

Fixes #66.

Behavior

  • After the collections, indexes and HasData rows exist, EnsureCreated runs the UseSeeding delegate and EnsureCreatedAsync runs UseAsyncSeeding. They run on every call, so a seed must be idempotent; the bool argument is true when the database did not exist before the call (the value EnsureCreated returns, so it is false when only some collections were missing).
  • With only the other kind of delegate configured, the call throws EF Core's MissingSeeder InvalidOperationException, as for relational providers.
  • Not wrapped in a transaction, unlike relational providers. EF's RelationalDatabaseCreator calls the seeder between BeginTransaction and Commit. Here that would make the delegate's own SaveChanges throw under EnableRetryOnFailure (a retrying strategy rejects a transaction begun outside it; see RetryOnFailureTests), and would need a replica set even for a single-root seed. The delegate's SaveChanges takes its usual implicit transaction instead.
  • MongoDatabaseCreator needs IDbContextOptions and ICurrentDbContext, so it gets a second public constructor. The released five-argument constructor is kept, so it is not a source break; a creator built with it runs no delegates.
  • EF8 has no seeding API, so all of this is under #if !EF8.

This changes observable behavior (a delegate that used to be ignored now runs, and the mismatched pairing now throws), so it is logged in BREAKING-CHANGES.md.

Tests

  • New EnsureCreatedSeedingTests (14 cases, sync and async): runs every time with the right flag, runs after HasData, works with EnableRetryOnFailure (checked from inside the delegate: retrying strategy active, no transaction open), the MissingSeeder cases both ways, exception propagation, the options overload, and the original constructor running nothing. 12 of the first 13 fail without the fix.
  • The explicit SeedAsync workarounds in OptimisticConcurrencyMongoTest, SerializationMongoTest and DataBindingMongoTest are removed (EF-X215 marked fixed in docs/failing-spec-tests.md).
  • test-all (EF8-EF11, Default and NativeOnly) passed before the review follow-ups and the last two rebases onto release. After those, on EF10 only, I re-ran the seeding and creator tests and the three fixture suites (all pass); the new tests also pass on EF9 and EF11. CI will run the full matrix on this head.

A review with the per-area and cross-cutting reviewers found no functional bugs; its follow-ups (the breaking-changes entry, wording of the flag, a stronger retry test, a test for the old constructor) are included.

🤖 Generated with Claude Code

…#66)

EnsureCreated and EnsureCreatedAsync ignored the delegates set with
UseSeeding / UseAsyncSeeding (EF9+). They now run last, after collections,
indexes and HasData rows exist, on every call, with a bool that is true when
the database did not exist (the value EnsureCreated returns). Configuring only
the other kind of delegate throws EF Core's MissingSeeder error, as for
relational providers.

Unlike relational providers the delegate is not wrapped in a transaction: a
retrying execution strategy rejects a transaction begun outside it, and the
wrapper would need a replica set even for a single-root seed. The delegate's
own SaveChanges takes its usual implicit transaction.

MongoDatabaseCreator gets a second public constructor taking IDbContextOptions
and ICurrentDbContext; the original constructor is kept (a creator built with
it runs no delegates). Logged in BREAKING-CHANGES.md as a behavior change.

The F1 spec fixtures no longer seed explicitly (EF-X215).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@ajcvickers
ajcvickers merged commit 0d4e2fd into release Nov 14, 2026
3 checks passed
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.

EnsureCreated does not run UseSeeding / UseAsyncSeeding

1 participant