Fix HasDiscriminator with default name "Discriminator" failing for non-string types#37785
Merged
AndriySvyryd merged 2 commits intomainfrom Feb 25, 2026
Merged
Conversation
…hanged Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix issue with .HasDiscriminator in EF Core 10
Fix HasDiscriminator with default name "Discriminator" failing for non-string types
Feb 24, 2026
There was a problem hiding this comment.
Pull request overview
This PR fixes a regression in EF Core 10 where calling HasDiscriminator<TEnum>("Discriminator") with the default discriminator column name fails during model validation because conventions incorrectly override the user-configured type back to string.
Changes:
- Fixed
TypeBase.SetDiscriminatorPropertyto update the annotation'sConfigurationSourcewhen taking the early return path for unchanged property names - Added regression test to verify discriminator with non-string types works with the default "Discriminator" name
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/EFCore/Metadata/Internal/TypeBase.cs | Updates ConfigurationSource when discriminator property name is unchanged to prevent conventions from overriding explicit configuration |
| test/EFCore.Tests/Infrastructure/ModelValidatorTest.cs | Adds regression test for using default discriminator name with non-string types |
roji
approved these changes
Feb 25, 2026
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.
Fixes #37571
When calling
.HasDiscriminator<TEnum>("Discriminator")using the default discriminator column name, EF Core 10 incorrectly throws during model validation because conventions override the user-configured type back tostring.Root cause
TypeBase.SetDiscriminatorPropertyhas an early return when the property name is unchanged (e.g., user specifies"Discriminator"and the convention-created default is also"Discriminator"). This early return skipped updating the annotation'sConfigurationSource, leaving it atConvention. When derived types were subsequently registered, conventions re-ran and, seeing aConvention-level annotation, successfully overrode the discriminator property type back tostring.Fix
TypeBase.SetDiscriminatorProperty: when taking the early return for an unchanged property name, also callUpdateConfigurationSourceon the existing annotation so the caller'sConfigurationSource(e.g.,Explicit) is properly propagated.Does_not_detect_missing_discriminator_values_when_using_default_discriminator_name_with_non_string_typetoModelValidatorTest.Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.