Scope the Postgres enum registrations to the EF Core service provider - #5
Merged
Merged
Conversation
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.
Summary
Makes
RawSqlEnumParameterTestsstop failing intermittently in CI with42804: column "Sport" is of type sport but expression is of type text.The store-type lookup lived in a process-wide static
StringEnumPgTypeRegistry, while EF Core memoises the firstFindMapping(typeof(TEnum), storeType: null)result in a cache owned by the internal service provider. BecauseStrEnumNpgsqlOptionsExtensionkeyed every configuration to the same provider, all four integration-test contexts shared one type-mapping cache. Whichever context built its model first won: if one that had not registered an enum got there first, EF memoised the miss and served it to the context that had registered, so raw-SQL parameters fell back totext. Test classes run in parallel, hence the flakiness.The fix scopes the registrations to the service provider that owns the cache:
StringEnumPostgresEnumRegistraraccumulates its registrations instead of writing to a static, and the options extension carries them.NpgsqlStringEnumTypeMappingSourcePlugintakes that map by constructor and resolves against it, so a plugin instance only ever sees its own context's registrations.StringEnumPgTypeRegistryis deleted;StringEnumPgTypeKeyholds the shared key format.One behaviour change worth noting:
HasPostgresStringEnum/MapStringEnumAsPostgresEnumno longer feed the raw-SQL parameter path, because model-level configuration runs insideOnModelCreating— after EF has already memoised the(TEnum, null)lookup. That path never worked reliably; it is the race this PR removes. Up-front registration viaUseStringEnumsAsPostgresEnums(r => r.MapStringEnum<TEnum>())remains the supported way to bind string enums on the raw-SQL path, exactly asRawSqlEnumParameterTestsalready does. Property-driven mapping is unaffected.Test plan
dotnet test— 29 unit and 6 integration tests passdocker build --target test .passesStrEnumNpgsqlOptionsExtensionTestscover provider keying: same registrations share a provider; differing registrations, differing enum names, and differing enums do notNpgsqlStringEnumTypeMappingSourcePluginTestsupdated to construct the plugin with explicit registrations rather than mutating global state