dotnet/efcore · error · InvalidOperationException

'UseChangeTrackingProxies' and 'UseLazyLoadingProxies' each

Error message

'UseChangeTrackingProxies' and 'UseLazyLoadingProxies' each require 'AddEntityFrameworkProxies' to be called on the internal service provider used.

What it means

Thrown by ProxyFactory.CreateLazyLoadingProxy when the ProxiesOptionsExtension is missing from the context's IDbContextOptions. The extension is registered by AddEntityFrameworkProxies; without it the lazy-loader, interceptors, and proxy generator services are not in the internal service provider.

Source

Thrown at src/EFCore.Proxies/Proxies/Internal/ProxyFactory.cs:78

            entityType.ClrType,
            GetInterfacesToProxy(entityType),
            GenerationOptions);

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual object CreateLazyLoadingProxy(
        DbContext context,
        IEntityType entityType,
        ILazyLoader loader,
        object[] constructorArguments)
    {
        var options = context.GetService<IDbContextOptions>().FindExtension<ProxiesOptionsExtension>();
        return options == null
            ? throw new InvalidOperationException(ProxiesStrings.ProxyServicesMissing)
            : CreateLazyLoadingProxy(entityType, loader, constructorArguments);
    }

    private object CreateLazyLoadingProxy(
        IEntityType entityType,
        ILazyLoader loader,
        object[] constructorArguments)
        => _generator.CreateClassProxy(
            entityType.ClrType,
            GetInterfacesToProxy(entityType),
            GenerationOptions,
            constructorArguments,
            GetNotifyChangeInterceptors(entityType, new LazyLoadingInterceptor(entityType, loader)));

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that

View on GitHub (pinned to dbf9771522)

Solutions

  1. Call options.UseInternalServiceProvider(...).AddEntityFrameworkProxies() when using a custom internal provider, OR call UseLazyLoadingProxies() on DbContextOptionsBuilder which wires it up.
  2. Ensure the EFCore.Proxies NuGet package is referenced.
  3. If using AddDbContext, chain .AddEntityFrameworkProxies() or use UseLazyLoadingProxies() in the options action.
  4. Do not share a single internal service provider across contexts unless proxies are registered for all of them.

Example fix

// before
services.AddDbContext<MyContext>(o => o.UseSqlServer(conn));
// later: proxy creation throws

// after
services.AddDbContext<MyContext>(o =>
    o.UseSqlServer(conn)
     .UseLazyLoadingProxies());
// or for custom provider:
options.UseInternalServiceProvider(sp);
services.AddEntityFrameworkProxies();
Defensive patterns

Strategy: validation

Validate before calling

// Verify the proxies extension is configured
var ext = options.Options.FindExtension<ProxiesOptionsExtension>();
if (ext == null)
    throw new InvalidOperationException("Call AddEntityFrameworkProxies / UseLazyLoadingProxies first.");

Type guard

static bool ProxiesRegistered(DbContextOptionsBuilder o)
    => o.Options.FindExtension<ProxiesOptionsExtension>() is not null;

Prevention

When it happens

Trigger: A proxy is requested via CreateLazyLoadingProxy (ProxyFactory.cs:76-78) on a context whose DbContextOptions does not contain a ProxiesOptionsExtension. Typically because AddEntityFrameworkProxies() / UseLazyLoadingProxies() was never called, or a custom internal service provider was supplied without it.

Common situations: Forgetting AddEntityFrameworkProxies on the IServiceCollection; using AddDbContext without the proxies call but calling proxy APIs; replacing the internal service provider (UseInternalServiceProvider) without registering proxies on it; version mismatch where the proxies package isn't referenced.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/d67fa0c45e4c5b4a. Report an issue: GitHub.