Repository navigation
EnsureCreated: run the UseSeeding and UseAsyncSeeding delegates (Fixes #66) - #115
Merged
Merged
Conversation
…#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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
EnsureCreatedandEnsureCreatedAsyncignored the delegates set withUseSeeding/UseAsyncSeeding(EF9+), so data seeded that way was never written. They now run them, as the relational providers do.Fixes #66.
Behavior
HasDatarows exist,EnsureCreatedruns theUseSeedingdelegate andEnsureCreatedAsyncrunsUseAsyncSeeding. They run on every call, so a seed must be idempotent; theboolargument istruewhen the database did not exist before the call (the valueEnsureCreatedreturns, so it isfalsewhen only some collections were missing).MissingSeederInvalidOperationException, as for relational providers.RelationalDatabaseCreatorcalls the seeder betweenBeginTransactionandCommit. Here that would make the delegate's ownSaveChangesthrow underEnableRetryOnFailure(a retrying strategy rejects a transaction begun outside it; seeRetryOnFailureTests), and would need a replica set even for a single-root seed. The delegate'sSaveChangestakes its usual implicit transaction instead.MongoDatabaseCreatorneedsIDbContextOptionsandICurrentDbContext, 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.#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
EnsureCreatedSeedingTests(14 cases, sync and async): runs every time with the right flag, runs afterHasData, works withEnableRetryOnFailure(checked from inside the delegate: retrying strategy active, no transaction open), theMissingSeedercases both ways, exception propagation, the options overload, and the original constructor running nothing. 12 of the first 13 fail without the fix.SeedAsyncworkarounds inOptimisticConcurrencyMongoTest,SerializationMongoTestandDataBindingMongoTestare removed (EF-X215 marked fixed indocs/failing-spec-tests.md).test-all(EF8-EF11, Default and NativeOnly) passed before the review follow-ups and the last two rebases ontorelease. 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