Repository navigation
Throw for change-tracking proxy properties whose setters cannot be overridden - #39592
Draft
ajcvickers wants to merge 9 commits into
Draft
ajcvickers wants to merge 9 commits into
ajcvickers wants to merge 9 commits into
Conversation
ajcvickers
force-pushed
the
fix-39589-internal-proxy-properties
branch
3 times, most recently
from
November 16, 2026 08:51
e0dc95e to
5e36ac9
Compare
ajcvickers
force-pushed
the
fix-39589-internal-proxy-properties
branch
2 times, most recently
from
December 1, 2026 19:31
ca7f929 to
0082f80
Compare
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
force-pushed
the
fix-39589-internal-proxy-properties
branch
from
December 2, 2026 13:02
0082f80 to
d03968b
Compare
…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
force-pushed
the
fix-39589-internal-proxy-properties
branch
from
December 2, 2026 15:57
d03968b to
fc68291
Compare
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 #39589
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 aspublic 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 throwsInvalidOperationExceptionwhenUseChangeTrackingProxiesis used and the setter ofcannot be overridden by the proxy. It uses the same Castle accessibility rules as #39591 (
ProxyUtil.IsAccessible), soInternalsVisibleTo("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.Save, such as computed columns and row versions.virtualcheck.Tests
ProxyAccessibilityTests(emitted types): scalar properties and the indexer at every accessibility level, with and withoutInternalsVisibleTo; lazy-loading does not validate them; and an end-to-end test that a visibleinternalsetter is intercepted, so the entity becomesModifiedwithoutDetectChanges.ChangeDetectionProxyTests(plain C# classes):{ get; internal set; }on a property and on a key throws, and{ get; protected set; }is accepted. Alsointernal init, a shared-type entity type, and a publicinit.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, withoutInternalsVisibleTo("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
virtualcheck: #39906.🤖 Generated with Claude Code