Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Throw for change-tracking proxy properties whose setters cannot be overridden - #39592

Draft
ajcvickers wants to merge 9 commits into
dotnet:mainfrom
ajcvickers:fix-39589-internal-proxy-properties
Draft

ajcvickers wants to merge 9 commits into
dotnet:mainfrom
ajcvickers:fix-39589-internal-proxy-properties

Conversation

@ajcvickers

@ajcvickers ajcvickers commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #39589

Draft: stacked on #39591. GitHub can't base this PR on another fork branch, so it targets main and temporarily includes the commit from #39591 (Validate that proxies can override navigation accessors). Only the second commit, Validate that change-tracking proxies can override property setters, is new here. I'll rebase onto main once #39591 merges, and the diff will then shrink to that commit.

Change-tracking proxies must override the setter of every mapped property to receive change notifications. A property whose setter is not visible to DynamicProxyGenAssembly2, such as public virtual string Name { get; internal set; }, passed model validation but was silently never tracked, so changes to it were not detected or saved. #39591 fixes the same problem for navigations.

Change

Model validation (ProxyBindingRewriter) now throws InvalidOperationException when UseChangeTrackingProxies is used and the setter of

  • a mapped scalar property, or
  • the mapped indexer property

cannot be overridden by the proxy. It uses the same Castle accessibility rules as #39591 (ProxyUtil.IsAccessible), so InternalsVisibleTo("DynamicProxyGenAssembly2") is honored. The scalar message is the one added in #39591 and names the property; the indexer has a new message, since the CLR indexer has no useful name.

  • Key properties and properties on owned types are validated like any other property.
  • Not validated, because the setter isn't intercepted or the change is never saved: entity types that raise both notifications themselves (as for navigations), keyless entity types, and properties that are never saved, meaning that both their before-save and after-save behavior aren't Save, such as computed columns and row versions.
  • Lazy-loading proxies are unaffected: they don't intercept scalar properties.
  • Getter-only properties are still skipped, as for the existing virtual check.

Tests

  • ProxyAccessibilityTests (emitted types): scalar properties and the indexer at every accessibility level, with and without InternalsVisibleTo; lazy-loading does not validate them; and an end-to-end test that a visible internal setter is intercepted, so the entity becomes Modified without DetectChanges.
  • ChangeDetectionProxyTests (plain C# classes): { get; internal set; } on a property and on a key throws, and { get; protected set; } is accepted. Also internal init, a shared-type entity type, and a public init.
  • Self-notifying entity types are accepted for properties and the indexer, and entity types that raise only one of the notifications still throw. Keyless entity types, computed columns and row versions are accepted, but a property that is saved only on insert or only on update still throws. Both kinds of proxy together, a property of an abstract base (documents the gap above) and of a non-abstract base entity type, and more accessibilities in the end-to-end test.

Verified with the Proxies tests (160), the InMemory, Sqlite and SQL Server proxy functional tests, and the AspNet identity and complex-type tracking tests. All pass.

Breaking change

Apps with public virtual T Prop { get; internal set; } (including keys) under change-tracking proxies, without InternalsVisibleTo("DynamicProxyGenAssembly2"), will now get a model-validation error instead of changes being silently lost. This also applies when the model is built at design time (dotnet ef migrations add, dbcontext optimize), and a compiled model that was generated before keeps not validating until it is regenerated. The docs breaking-change note (dotnet/EntityFramework.Docs#5515) is being extended to cover this.

Not in this PR

Entity types that are abstract are skipped by all of the rewriter's checks, so an abstract base with an invisible setter is still not validated. This is an existing gap that also applies to navigations and the virtual check: #39906.

🤖 Generated with Claude Code

@ajcvickers
ajcvickers force-pushed the fix-39589-internal-proxy-properties branch 3 times, most recently from e0dc95e to 5e36ac9 Compare November 16, 2026 08:51
@ajcvickers
ajcvickers force-pushed the fix-39589-internal-proxy-properties branch 2 times, most recently from ca7f929 to 0082f80 Compare December 1, 2026 19:31
ajcvickers and others added 4 commits December 2, 2026 12:18
Castle proxies cannot override members that are not visible to
DynamicProxyGenAssembly2, such as internal virtual navigations, unless the
declaring assembly has [InternalsVisibleTo("DynamicProxyGenAssembly2")].
Such navigations passed model validation but were silently never
lazy-loaded or change-tracked.

Model validation now throws when the getter (lazy-loading proxies) or the
setter (change-tracking proxies) of a navigation cannot be overridden,
using Castle's own accessibility rules. IgnoreNonVirtualNavigations and
EnableLazyLoading(false) still suppress the check for lazy-loading.
Entity types that are themselves inaccessible are not reported here, since
they already fail when a proxy is created.

Fixes dotnet#32352

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The generated NonVisibleProperty method had its parameters hand-reordered
relative to what tools/Resources.tt produces, so regenerating would have
swapped the entity type and property in the message. Use numbered
placeholders in the resx so the generated order matches, and assert the
literal message prefix in the tests.

The InternalsVisibleTo advice now says the assembly that declares the
property, since that is the assembly Castle checks, not the one declaring
the entity type.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Split the NonVisibleProperty message into NonVisibleGetter and NonVisibleSetter so the whole
sentence is in the resource, mention protected internal, and point lazy-loading users at the
EnableLazyLoading(false) / IgnoreNonVirtualNavigations opt-outs.

Tests: explain why the private cases are emitted as IL, and add private protected end-to-end
cases, skip navigations, and a navigation declared on a base type in another assembly.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
When the entity type implements both INotifyPropertyChanged and
INotifyPropertyChanging, change-tracking proxies add no notification
interceptors, so the proxy never overrides the navigation setter and it
does not need to be visible to DynamicProxyGenAssembly2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ajcvickers and others added 5 commits December 2, 2026 15:53
…e the visibility messages

Entities of a keyless entity type are never tracked, so the setters of their navigations are never intercepted for change tracking. The messages now say that the property, not only its accessor, has to be made more visible, and the setter message mentions implementing both notification interfaces.

Tests: both kinds of proxy together, the getter check on self-notifying entity types, change tracking of a navigation with lazy-loading disabled, owned navigations and keyless entity types.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Change-tracking proxies must override the setter of every mapped property
to receive change notifications. A property whose setter is not visible to
DynamicProxyGenAssembly2, such as `public virtual string Name { get; internal set; }`,
passed model validation but was silently never tracked, so changes to it
were not detected or saved.

Model validation now throws when the setter of a mapped scalar property or
of the mapped indexer property cannot be overridden by the proxy, using the
same Castle accessibility rules as for navigations. Key properties are
validated like any other property. Lazy-loading proxies are unaffected.

Fixes dotnet#39589

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Say that the InternalsVisibleTo grant goes on the assembly that declares the
indexer, since that is the assembly Castle checks. Cover the exact repro from
dotnet#39589 (an internal virtual property), owned types, derived types and a setter
inherited from an unmapped base type, and correct two stale test comments.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…notifying entity types

An entity type that raises both notifications itself gets no notification interceptors, so the proxy doesn't override the setters of its properties or its indexer, and they don't need to be visible to the proxy generator. Navigation setters were already exempt.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ies that are never saved, and add tests

Entities of a keyless entity type are never tracked, and a property that is never saved, like a computed column or a row version, is not changed by the application, so the visibility of their setters doesn't matter. The indexer message now also says that the indexer has to be made more visible and mentions implementing both notification interfaces.

Members of abstract entity types are still not validated (dotnet#39906), which is now noted in the code and covered by a test.

Tests: keyless entity types, computed and row version properties, properties that are saved at one point, both kinds of proxy together, a property of an abstract and of a non-abstract base entity type, init-only setters, shared-type entity types, and more accessibilities in the end-to-end test.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@ajcvickers
ajcvickers force-pushed the fix-39589-internal-proxy-properties branch from d03968b to fc68291 Compare December 2, 2026 15:57
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.

Change-tracking proxies silently ignore internal virtual properties

1 participant