Skip to content

Commit

Permalink
Don't proxy navigations not configured for lazy loading
Browse files Browse the repository at this point in the history
Part of #10787
  • Loading branch information
ajcvickers committed Jan 1, 2023
1 parent 38f69c6 commit 803a84e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/EFCore.Proxies/Proxies/Internal/ProxyBindingRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
23 changes: 23 additions & 0 deletions test/EFCore.Proxies.Tests/LazyLoadingProxyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -108,6 +116,21 @@ public LazyContext()
}
}

private class LazyContextDisabledNavigation : TestContext<LazyNonVirtualNavEntity>
{
public LazyContextDisabledNavigation()
: base(dbName: "LazyLoadingContext", useLazyLoading: true, useChangeDetection: false)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<LazyNonVirtualNavEntity>().Navigation(e => e.SelfRef).EnableLazyLoading(false);
}
}

public sealed class LazySealedEntity
{
public int Id { get; set; }
Expand Down

0 comments on commit 803a84e

Please sign in to comment.