Skip to content

Throw for unions mapped as entity or complex types#38398

Open
Copilot wants to merge 4 commits into
mainfrom
copilot/throw-for-unions
Open

Throw for unions mapped as entity or complex types#38398
Copilot wants to merge 4 commits into
mainfrom
copilot/throw-for-unions

Conversation

Copilot AI commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

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

  • Detection (SharedTypeExtensions): added Type.IsUnion(), which checks for System.Runtime.CompilerServices.UnionAttribute (matched by full name to avoid a compile-time dependency on the marker type).
  • Validation (ModelValidator): throw CoreStrings.UnionTypeNotSupported from ValidateEntityType (union entity types) and ValidatePropertyMapping(IComplexProperty) (union complex types; also covers complex collections and value-type unions).
  • Resource: new UnionTypeNotSupported string in CoreStrings.
  • Tests: ModelValidatorTest cases for union entity and complex types, simulated via the real [Union] attribute.

Message

The type '{type}' cannot be mapped because union types are not supported. See #36375 for more information.

Notes

  • Applies across all providers via the shared ModelValidator.
  • Detection keys off the canonical [Union] marker, so it covers both class and struct unions.

Copilot AI linked an issue Jun 10, 2026 that may be closed by this pull request
Copilot AI and others added 2 commits June 10, 2026 18:17
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
Copilot AI requested a review from AndriySvyryd June 10, 2026 18:23
@AndriySvyryd AndriySvyryd requested a review from Copilot June 10, 2026 19:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in SharedTypeExtensions (attribute full-name match to avoid hard dependency).
  • Throw CoreStrings.UnionTypeNotSupported(...) from ModelValidator when 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

Comment thread src/EFCore/Properties/CoreStrings.Designer.cs
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI requested a review from AndriySvyryd June 10, 2026 21:19
@AndriySvyryd AndriySvyryd marked this pull request as ready for review June 10, 2026 22:05
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner June 10, 2026 22:05
Copilot AI review requested due to automatic review settings June 10, 2026 22:05
@AndriySvyryd AndriySvyryd requested a review from cincuranet June 10, 2026 22:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 6 changed files in this pull request and generated no new comments.

Files not reviewed (1)
  • src/EFCore/Properties/CoreStrings.Designer.cs: Language not supported

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Throw for unions

3 participants