{"record":{"id":"4228159e59b647f9","repo":"dotnet/efcore","slug":"usechangetrackingproxies-and-uselazyloadingprox","errorCode":null,"errorMessage":"'UseChangeTrackingProxies' and 'UseLazyLoadingProxies' each require 'AddEntityFrameworkProxies' to be called on the internal service provider used.","messagePattern":"'UseChangeTrackingProxies' and 'UseLazyLoadingProxies' each require 'AddEntityFrameworkProxies' to be called on the internal service provider used\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Proxies/Proxies/Internal/ProxiesOptionsExtension.cs","lineNumber":173,"sourceCode":"\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual void Validate(IDbContextOptions options)\n    {\n        if (UseProxies)\n        {\n            var internalServiceProvider = options.FindExtension<CoreOptionsExtension>()?.InternalServiceProvider;\n            if (internalServiceProvider != null)\n            {\n                using var scope = internalServiceProvider.CreateScope();\n                var conventionPlugins = scope.ServiceProvider.GetService<IEnumerable<IConventionSetPlugin>>();\n                if (conventionPlugins?.Any(s => s is ProxiesConventionSetPlugin) == false)\n                {\n                    throw new InvalidOperationException(ProxiesStrings.ProxyServicesMissing);\n                }\n            }\n        }\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual void ApplyServices(IServiceCollection services)\n        => services.AddEntityFrameworkProxies();\n\n    private sealed class ExtensionInfo(IDbContextOptionsExtension extension) : DbContextOptionsExtensionInfo(extension)\n    {\n        private string? _logFragment;\n","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Proxies/Proxies/Internal/ProxiesOptionsExtension.cs#L155-L191","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Call services.AddEntityFrameworkProxies() on the same IServiceCollection passed to AddEntityFramework(...) / UseInternalServiceProvider(...).","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.","Ensure AddEntityFrameworkProxies is called before the internal provider is built (before BuildServiceProvider)."],"exampleFix":"// before — internal provider missing proxies services\nvar services = new ServiceCollection();\nservices.AddEntityFrameworkInMemoryDatabase();\nservices.AddDbContext<MyContext>(opt =>\n    opt.UseInternalServiceProvider(services.BuildServiceProvider())\n       .UseInMemoryDatabase(\"db\")\n       .UseLazyLoadingProxies()); // throws\n\n// after — register proxies on the service collection\nvar services = new ServiceCollection();\nservices.AddEntityFrameworkInMemoryDatabase();\nservices.AddEntityFrameworkProxies();\nvar provider = services.BuildServiceProvider();\nservices.AddDbContext<MyContext>(opt =>\n    opt.UseInternalServiceProvider(provider)\n       .UseInMemoryDatabase(\"db\")\n       .UseLazyLoadingProxies());","handlingStrategy":"validation","validationCode":"// At startup, verify proxies services are registered on the internal provider.\nusing var scope = internalServiceProvider.CreateScope();\nvar plugins = scope.ServiceProvider.GetService<IEnumerable<IConventionSetPlugin>>();\nif (useProxies && plugins?.Any(p => p is ProxiesConventionSetPlugin) != true)\n{\n    throw new InvalidOperationException(\n        \"AddEntityFrameworkProxies() must be called on the service collection \" +\n        \"used for UseInternalServiceProvider when proxies are enabled.\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    using var context = new MyContext(options);\n    context.Database.EnsureCreated();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"AddEntityFrameworkProxies\"))\n{\n    // Re-register: add AddEntityFrameworkProxies to the service collection and rebuild\n    throw new ConfigurationException(\n        \"Proxies services missing. Call AddEntityFrameworkProxies() on your IServiceCollection.\", ex);\n}","preventionTips":["When using UseInternalServiceProvider, always call AddEntityFrameworkProxies() on the same IServiceCollection.","Register AddEntityFrameworkProxies() before BuildServiceProvider().","Prefer letting EF Core manage the internal provider unless sharing is explicitly required.","Add a startup health check that validates proxy service registration."],"tags":["efcore","proxies","di","service-provider","configuration"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}