Skip to content

Add proper handling when a property’s backing field cannot be inferred or the unsafe accessor cannot be generated.#36720

Merged
AndriySvyryd merged 1 commit intodotnet:mainfrom
kawasaniac:fix35219
Feb 28, 2026
Merged

Add proper handling when a property’s backing field cannot be inferred or the unsafe accessor cannot be generated.#36720
AndriySvyryd merged 1 commit intodotnet:mainfrom
kawasaniac:fix35219

Conversation

@kawasaniac
Copy link
Contributor

@kawasaniac kawasaniac commented Sep 7, 2025

My take on adding additional handling for the following issue: #35219

Throws InvalidOperationException with proper message as in DesignStrings (CompiledModelBackingFieldNotFound and CompiledModelUnsafeAccessorNull). Plus added additional exceptions specifically for this case.

  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

@kawasaniac kawasaniac requested a review from a team as a code owner September 7, 2025 18:45
@kawasaniac
Copy link
Contributor Author

@dotnet-policy-service agree

@AndriySvyryd AndriySvyryd self-assigned this Sep 8, 2025
Copy link
Member

@AndriySvyryd AndriySvyryd left a comment

Choose a reason for hiding this comment

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

@AndriySvyryd AndriySvyryd changed the base branch from release/10.0 to main September 19, 2025 03:18
@roji roji force-pushed the main branch 2 times, most recently from 249ae47 to 6b86657 Compare January 13, 2026 17:46
Copilot AI review requested due to automatic review settings February 27, 2026 00:52
@AndriySvyryd AndriySvyryd enabled auto-merge (squash) February 27, 2026 00:55
@AndriySvyryd
Copy link
Member

Thanks for your contribution

Copy link

Copilot AI left a comment

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 improves design-time compiled model generation (especially for --nativeaot) by replacing a NullReferenceException failure mode with a targeted InvalidOperationException and a new DesignStrings message when EF can’t determine the backing member to use.

Changes:

  • Add a new design-time resource string CompiledModelBackingFieldNotFound and expose it via DesignStrings.
  • Update CSharpRuntimeModelCodeGenerator to throw a clearer InvalidOperationException instead of flowing a null MemberInfo into unsafe-accessor name generation.
  • Add a new compiled-model scaffolding test covering the backing-field-not-found scenario.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.

File Description
test/EFCore.Specification.Tests/Scaffolding/CompiledModelTestBase.cs Adds a NativeAOT test case expecting the new error message and a new entity type used by that test.
src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs Adds a guard/exception when GetMemberInfo(...) yields null during unsafe-accessor registration.
src/EFCore.Design/Properties/DesignStrings.resx Adds CompiledModelBackingFieldNotFound string (and reorders/moves some existing entries).
src/EFCore.Design/Properties/DesignStrings.Designer.cs Adds the strongly-typed accessor for CompiledModelBackingFieldNotFound (and reflects resource reordering).
Files not reviewed (1)
  • src/EFCore.Design/Properties/DesignStrings.Designer.cs: Language not supported

Copilot AI review requested due to automatic review settings February 27, 2026 01:02
Copy link

Copilot AI left a comment

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 3 out of 4 changed files in this pull request and generated 2 comments.

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

@AndriySvyryd AndriySvyryd enabled auto-merge (squash) February 27, 2026 21:40
@AndriySvyryd AndriySvyryd merged commit 15af73b into dotnet:main Feb 28, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NullReferenceException when trying to optimize a DbContext with --nativeaot

3 participants