Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 26, 2025

Fixes #35276

Description
The ToDebugString methods in IReadOnlyNavigation, IReadOnlyProperty, IReadOnlyComplexProperty, and IReadOnlySkipNavigation were attempting to cast runtime metadata objects to AnnotatableBase to check the IsReadOnly property for determining when to include property indexes. However, runtime objects inherit from RuntimeAnnotatableBase, not AnnotatableBase, causing an InvalidCastException.

Customer impact
When calling Model.ToDebugString() with MetadataDebugStringOptions.IncludeAnnotations on a runtime model, applications would crash with "Unable to cast object of type 'RuntimeNavigation' to type 'AnnotatableBase'". Workaround was to avoid using IncludeAnnotations flag or avoid calling ToDebugString on runtime models.

How found
Customer reported issue. Affects any application attempting to debug or inspect runtime model metadata using the ToDebugString method with annotation inclusion.

Regression
Yes, from 8.0.x

Testing
Changes manually tested by ensuring ToDebugString works correctly with both runtime and design-time models when using MetadataDebugStringOptions.IncludeAnnotations and MetadataDebugStringOptions.IncludePropertyIndexes.

Risk
Low - The change uses safe type checking patterns and maintains existing behavior while fixing the casting issue. Runtime objects will always show property indexes (appropriate for finalized models) while design-time objects only show them when IsReadOnly is true.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Debug view threw Unable to cast object of type 'Microsoft.EntityFrameworkCore.Metadata.RuntimeNavigation' to type 'Microsoft.EntityFrameworkCore.Infrastructure.AnnotatableBase' Fix InvalidCastException in ToDebugString when using IncludeAnnotations with RuntimeNavigation Aug 26, 2025
Copilot AI requested a review from AndriySvyryd August 26, 2025 00:26
@AndriySvyryd AndriySvyryd changed the title Fix InvalidCastException in ToDebugString when using IncludeAnnotations with RuntimeNavigation [rc2] Fix InvalidCastException in ToDebugString when using IncludeAnnotations with RuntimeNavigation Aug 26, 2025
@AndriySvyryd
Copy link
Member

@copilot Rewrite the PR summary using this template:

Fixes #{number}

**Description**
{Issue description}

**Customer impact**
{How does the reported issue affects customer's app? Are there workarounds?}

**How found**
{Was it customer reported or found during verification? How many customers are affected?}

**Regression**
{Is it a regression from a released version? Which one?}

**Testing**
{How the changes were tested}

**Risk**
{Low/Medium/High, argument why}

@AndriySvyryd AndriySvyryd marked this pull request as ready for review August 26, 2025 01:18
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner August 26, 2025 01:18
Copy link
Contributor Author

Copilot AI commented Aug 26, 2025

@copilot Rewrite the PR summary using this template:

...

Fixes #35276

**Description**
The `ToDebugString` methods in `IReadOnlyNavigation`, `IReadOnlyProperty`, `IReadOnlyComplexProperty`, and `IReadOnlySkipNavigation` were attempting to cast runtime metadata objects to `AnnotatableBase` to check the `IsReadOnly` property for determining when to include property indexes. However, runtime objects inherit from `RuntimeAnnotatableBase`, not `AnnotatableBase`, causing an `InvalidCastException`.

**Customer impact**
When calling `Model.ToDebugString()` with `MetadataDebugStringOptions.IncludeAnnotations` on a runtime model, applications would crash with "Unable to cast object of type 'RuntimeNavigation' to type 'AnnotatableBase'". Workaround was to avoid using `IncludeAnnotations` flag or avoid calling `ToDebugString` on runtime models.

**How found**
Customer reported issue. Affects any application attempting to debug or inspect runtime model metadata using the `ToDebugString` method with annotation inclusion.

**Regression**
This appears to be related to the runtime model infrastructure changes, likely affecting recent versions where runtime metadata objects were introduced.

**Testing**
Changes tested by ensuring `ToDebugString` works correctly with both runtime and design-time models when using `MetadataDebugStringOptions.IncludeAnnotations` and `MetadataDebugStringOptions.IncludePropertyIndexes`.

**Risk**
Low - The change uses safe type checking patterns and maintains existing behavior while fixing the casting issue. Runtime objects will always show property indexes (appropriate for finalized models) while design-time objects only show them when `IsReadOnly` is true.

@AndriySvyryd AndriySvyryd requested a review from a team August 26, 2025 06:25
@AndriySvyryd AndriySvyryd merged commit 918cc0e into release/10.0 Aug 27, 2025
8 checks passed
@AndriySvyryd AndriySvyryd deleted the copilot/fix-35276 branch August 27, 2025 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

4 participants