The in-memory fixture the documentation recommends for tests failed at the first save - #203
Merged
Conversation
…t the first save UseWitDbInMemory() configured the context with a connection STRING, and an in-memory database is private to its connection - WitDbConnection.cs:115 says so in as many words. Entity Framework opens and closes a connection for every operation, so every operation got a fresh empty database. Measured: EnsureCreated() returns true and the very next SaveChanges throws DbUpdateException -> WitDbException: Table 'Items' not found. The fixture has never been able to save a row. The provider now creates the connection, OPENS it, and hands the open connection to the extension through the WithConnection path that was already there for UseWitDb(connection). EF does not close a connection it did not open, so the store outlives an operation. This is the recipe SQLite's provider documents, and for the same reason. The July audit sized this at two days and listed two routes - named shared memory, or a no-op Close for in-memory in WitRelationalConnection; supplying an open connection is a third one and it is an afternoon. Nothing disposes that connection, so the database lives as long as the options object and is collected with it. That is right for a test fixture and wrong for anything long-lived, so it is written into the XML comment rather than left to be discovered, and an overload taking a WitDbConnection the caller owns sits beside it for when the lifetime has to be in somebody's hands. InMemoryTests carried the note "these tests verify configuration and tracking, not full database execution", and that sentence was the defect: eighteen cases over the fixture and not one of them wrote a row. Three cases now do. The round trip is red before the fix with Table 'Entities' not found, and so it is with only the Open() removed - both parts of the fix have their own red. Two controls decide whether the fix is the right one: two fixtures must remain two databases, because sharing one store globally would pass the round trip perfectly and make every test in a suite a neighbour of every other; and an in-memory fixture must still write no file, because the creator decides "in memory" by reading the connection string and a fix that changed it would quietly leave databases on disk. UseWitDbInMemoryConfiguresExtensionTest asserted the connection string, which was true and was the defect. It asserts the open connection now, and says why in its own remarks. EF 593 cases, up from 590. Co-Authored-By: Claude Opus 5 (1M context) <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.
First of the fixes planned in
Docs/FIX-PLAN-FROM-SITE-FINDINGS-2026-08-15.md, and the one that costs a user most: the testing path three pages of the documentation recommend does not survive its firstSaveChanges.Measured
EnsureCreated()reports success and the very next operation cannot find the table it just made.Why
UseWitDbInMemory()configured the context with a connection string. An in-memory database is private to its connection —WitDbConnection.cs:115says so in as many words — and EF opens and closes a connection for every operation (WitDatabaseCreatordoes it explicitly aroundEnsureCreated). So every operation got a fresh, empty database.The fix
The provider creates the connection, opens it, and hands the open connection to the extension through the
WithConnectionpath that already existed forUseWitDb(connection). EF does not close a connection it did not open, so the store outlives an operation. Same recipe SQLite's provider documents, same reason.The July audit sized this at two days and listed two routes — named shared memory, or a no-op
Close()for in-memory inWitRelationalConnection. Supplying an open connection is a third, and it is an afternoon.Ownership is stated rather than left to be discovered. Nothing disposes that connection, so the database lives as long as the options object and is collected with it — right for a test fixture, wrong for anything long-lived. It is in the XML comment, and an overload taking a
WitDbConnectionthe caller owns sits beside it.The guards, and what each is for
InMemoryTestscarried the note "these tests verify configuration and tracking, not full database execution", and that sentence was the defect: eighteen cases over this fixture and not one of them wrote a row.AnInMemoryFixtureKeepsItsRowsAcrossOperationsTestEnsureCreated→SaveChanges→ read back in the same context → read back in a second context. Red before the fix, and red again with only theOpen()removed — both parts of the fix have their own redControlTwoInMemoryFixturesAreTwoDatabasesTestControlAnInMemoryFixtureWritesNoFileTestUseWitDbInMemoryConfiguresExtensionTestassertedConnectionString == "Data Source=:memory:", which was true and was the defect. It asserts the open connection now, with the reason in its own remarks —RelationalOptionsExtension.ConnectionStringis null in the connection-supplied shape by design, as it is for every other provider.EF 593 cases, up from 590.
🤖 Generated with Claude Code