From 803a84eedd5719bff5d3c7e7bd6b9b59ff508f58 Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Sun, 1 Jan 2023 11:31:47 +0000 Subject: [PATCH] Don't proxy navigations not configured for lazy loading Part of #10787 --- .../Proxies/Internal/ProxyBindingRewriter.cs | 3 ++- .../LazyLoadingProxyTests.cs | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/EFCore.Proxies/Proxies/Internal/ProxyBindingRewriter.cs b/src/EFCore.Proxies/Proxies/Internal/ProxyBindingRewriter.cs index 509b588d3f1..589a0d0f928 100644 --- a/src/EFCore.Proxies/Proxies/Internal/ProxyBindingRewriter.cs +++ b/src/EFCore.Proxies/Proxies/Internal/ProxyBindingRewriter.cs @@ -92,7 +92,8 @@ public virtual void ProcessModelFinalizing( ProxiesStrings.NonVirtualProperty(navigationBase.Name, entityType.DisplayName())); } - if (_options.UseLazyLoadingProxies) + if (_options.UseLazyLoadingProxies + && navigationBase.LazyLoadingEnabled) { if (!navigationBase.PropertyInfo.GetMethod!.IsReallyVirtual()) { diff --git a/test/EFCore.Proxies.Tests/LazyLoadingProxyTests.cs b/test/EFCore.Proxies.Tests/LazyLoadingProxyTests.cs index e132a868db0..d38f6f8964e 100644 --- a/test/EFCore.Proxies.Tests/LazyLoadingProxyTests.cs +++ b/test/EFCore.Proxies.Tests/LazyLoadingProxyTests.cs @@ -36,6 +36,14 @@ public void Does_not_throw_if_non_virtual_navigation_to_non_owned_type_is_allowe context.Model.FindEntityType(typeof(LazyNonVirtualNavEntity))!.FindNavigation(nameof(LazyNonVirtualNavEntity.SelfRef))); } + [ConditionalFact] + public void Does_not_throw_if_non_virtual_navigation_is_set_to_not_eager_load() + { + using var context = new LazyContextDisabledNavigation(); + Assert.NotNull( + context.Model.FindEntityType(typeof(LazyNonVirtualNavEntity))!.FindNavigation(nameof(LazyNonVirtualNavEntity.SelfRef))); + } + [ConditionalFact] public void Does_not_throw_if_non_virtual_navigation_to_owned_type() { @@ -108,6 +116,21 @@ public LazyContext() } } + private class LazyContextDisabledNavigation : TestContext + { + public LazyContextDisabledNavigation() + : base(dbName: "LazyLoadingContext", useLazyLoading: true, useChangeDetection: false) + { + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.Entity().Navigation(e => e.SelfRef).EnableLazyLoading(false); + } + } + public sealed class LazySealedEntity { public int Id { get; set; }