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

ProxiesOptionsExtension.Validate checks whether the internal service provider (if a custom one was supplied via UseInternalServiceProvider) contains a ProxiesConventionSetPlugin among its registered IConventionSetPlugin services. If proxies are enabled (UseChangeTrackingProxies or UseLazyLoadingProxies) but the plugin is missing, it throws ProxyServicesMissing. This means AddEntityFrameworkProxies was not called on the service collection that built the internal provider.

Solutions

  1. Call services.AddEntityFrameworkProxies() on the same IServiceCollection passed to AddEntityFramework(...) / UseInternalServiceProvider(...).
  2. If you do not need a shared internal provider, remove UseInternalServiceProvider so EF Core manages the provider and auto-registers proxies services when UseChangeTrackingProxies/UseLazyLoadingProxies is called.
  3. Ensure AddEntityFrameworkProxies is called before the internal provider is built (before BuildServiceProvider).

Example fix

// before — internal provider missing proxies services
var services = new ServiceCollection();
services.AddEntityFrameworkInMemoryDatabase();
services.AddDbContext<MyContext>(opt =>
    opt.UseInternalServiceProvider(services.BuildServiceProvider())
       .UseInMemoryDatabase("db")
       .UseLazyLoadingProxies()); // throws

// after — register proxies on the service collection
var services = new ServiceCollection();
services.AddEntityFrameworkInMemoryDatabase();
services.AddEntityFrameworkProxies();
var provider = services.BuildServiceProvider();
services.AddDbContext<MyContext>(opt =>
    opt.UseInternalServiceProvider(provider)
       .UseInMemoryDatabase("db")
       .UseLazyLoadingProxies());
Defensive patterns

Strategy: validation

Validate before calling

// At startup, verify proxies services are registered on the internal provider.
using var scope = internalServiceProvider.CreateScope();
var plugins = scope.ServiceProvider.GetService<IEnumerable<IConventionSetPlugin>>();
if (useProxies && plugins?.Any(p => p is ProxiesConventionSetPlugin) != true)
{
    throw new InvalidOperationException(
        "AddEntityFrameworkProxies() must be called on the service collection " +
        "used for UseInternalServiceProvider when proxies are enabled.");
}

Try / catch

try
{
    using var context = new MyContext(options);
    context.Database.EnsureCreated();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("AddEntityFrameworkProxies"))
{
    // Re-register: add AddEntityFrameworkProxies to the service collection and rebuild
    throw new ConfigurationException(
        "Proxies services missing. Call AddEntityFrameworkProxies() on your IServiceCollection.", ex);
}

Prevention

When it happens

Trigger: Calling UseChangeTrackingProxies() or UseLazyLoadingProxies() on DbContextOptions while also using a shared internal service provider (UseInternalServiceProvider) that was built without calling AddEntityFrameworkProxies() on its IServiceCollection. Validate runs during options validation at context construction.

Common situations: Sharing a single internal service provider across multiple DbContext types (common in ASP.NET Core or performance-sensitive apps) and forgetting to register the proxies services on it. Adding proxies to an existing app that already uses UseInternalServiceProvider without updating the service registration.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/4228159e59b647f9. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Proxies/Proxies/Internal/ProxiesOptionsExtension.cs:173

    /// <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 void Validate(IDbContextOptions options)
    {
        if (UseProxies)
        {
            var internalServiceProvider = options.FindExtension<CoreOptionsExtension>()?.InternalServiceProvider;
            if (internalServiceProvider != null)
            {
                using var scope = internalServiceProvider.CreateScope();
                var conventionPlugins = scope.ServiceProvider.GetService<IEnumerable<IConventionSetPlugin>>();
                if (conventionPlugins?.Any(s => s is ProxiesConventionSetPlugin) == false)
                {
                    throw new InvalidOperationException(ProxiesStrings.ProxyServicesMissing);
                }
            }
        }
    }

    /// <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 void ApplyServices(IServiceCollection services)
        => services.AddEntityFrameworkProxies();

    private sealed class ExtensionInfo(IDbContextOptionsExtension extension) : DbContextOptionsExtensionInfo(extension)
    {
        private string? _logFragment;

View on GitHub (pinned to 3a2006ef56)