Repository navigation
Throw for proxy navigations whose accessors cannot be overridden - #39591
Open
ajcvickers wants to merge 1 commit into
Open
ajcvickers wants to merge 1 commit into
ajcvickers wants to merge 1 commit into
Conversation
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>
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 #32352
Castle proxies cannot override members that are not visible to
DynamicProxyGenAssembly2, such asinternal virtualnavigations, unless the declaring assembly has[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]. Model validation only checked that the accessor wasvirtual, so these navigations passed validation and were then silently never lazy-loaded or change-tracked.Change
Model validation (
ProxyBindingRewriter) now throwsInvalidOperationExceptionwhen:UseLazyLoadingProxies: the getter of a lazy-loaded navigation cannot be overridden.UseChangeTrackingProxies: the setter of a navigation cannot be overridden. This includes collections with{ get; internal set; }.The check uses Castle's own
ProxyUtil.IsAccessible, so it follows exactly what the proxy generator can override, including theInternalsVisibleTocase. The message says whether the getter or the setter is the problem and how to fix it.Not affected, and covered by tests:
IgnoreNonVirtualNavigations()andEnableLazyLoading(false)still suppress the check for lazy-loading proxies, and owned navigations are still skipped.{ get; internal set; }keeps working.Tests
ProxyAccessibilityTestsemits entity types at runtime so that every accessibility level (public, protected, protected internal, internal, private protected, private) can be tested with and withoutInternalsVisibleTo("DynamicProxyGenAssembly2"), for references and collections. It includes end-to-end tests that a visibleinternalaccessor is really intercepted (lazy-loading loads the navigation, change-tracking detects the change), and one documenting the original symptom with validation suppressed.test.cmdwas run. The only failures were five many-to-many tests (ManyM_DB/Organisationstables missing or already existing) caused by parallel runs sharing a database; they pass when run on their own.Breaking change
Apps with
internal virtualnavigations, or{ get; internal set; }collections under change-tracking proxies, without theInternalsVisibleToattribute will now get a model-validation error instead of a silently broken navigation. A docs breaking-change note is being sent to dotnet/EntityFramework.Docs.Not in this PR
🤖 Generated with Claude Code