Throw for unions mapped as entity or complex types#38398
Open
Copilot wants to merge 4 commits into
Open
Conversation
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Throw error for unions as entity types in EF
Throw for unions mapped as entity or complex types
Jun 10, 2026
There was a problem hiding this comment.
Pull request overview
This PR adds first-class model validation for upcoming C# union types by detecting [System.Runtime.CompilerServices.Union] / UnionAttribute on CLR types and throwing a clear, user-facing error when such types are mapped as EF entity or complex types (aligning behavior across all providers).
Changes:
- Add
Type.IsUnion()detection inSharedTypeExtensions(attribute full-name match to avoid hard dependency). - Throw
CoreStrings.UnionTypeNotSupported(...)fromModelValidatorwhen unions are encountered as entity or complex types. - Add the new resource string and unit tests covering both entity-type and complex-type mapping scenarios.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| test/EFCore.Tests/Infrastructure/ModelValidatorTest.cs | Adds regression tests asserting unions mapped as entity/complex types produce the new clear validation error. |
| src/Shared/SharedTypeExtensions.cs | Introduces IsUnion() extension method based on UnionAttribute full-name detection. |
| src/EFCore/Infrastructure/ModelValidator.cs | Enforces the new validation rule by throwing UnionTypeNotSupported for union entity/complex types. |
| src/EFCore/Properties/CoreStrings.resx | Adds the user-facing error message resource for unsupported union mapping. |
| src/EFCore/Properties/CoreStrings.Designer.cs | Adds the generated accessor for UnionTypeNotSupported. |
Files not reviewed (1)
- src/EFCore/Properties/CoreStrings.Designer.cs: Language not supported
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.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.
Fixes #38376
EF doesn't support C# union types yet. Mapping one as an entity type or complex type currently produces undefined/confusing behavior instead of a clear error.
Changes
SharedTypeExtensions): addedType.IsUnion(), which checks forSystem.Runtime.CompilerServices.UnionAttribute(matched by full name to avoid a compile-time dependency on the marker type).ModelValidator): throwCoreStrings.UnionTypeNotSupportedfromValidateEntityType(union entity types) andValidatePropertyMapping(IComplexProperty)(union complex types; also covers complex collections and value-type unions).UnionTypeNotSupportedstring inCoreStrings.ModelValidatorTestcases for union entity and complex types, simulated via the real[Union]attribute.Message
Notes
ModelValidator.[Union]marker, so it covers bothclassandstructunions.