{"record":{"id":"d67fa0c45e4c5b4a","repo":"dotnet/efcore","slug":"usechangetrackingproxies-and-uselazyloadingprox-d67fa0","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/ProxyFactory.cs","lineNumber":78,"sourceCode":"            entityType.ClrType,\n            GetInterfacesToProxy(entityType),\n            GenerationOptions);\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 object CreateLazyLoadingProxy(\n        DbContext context,\n        IEntityType entityType,\n        ILazyLoader loader,\n        object[] constructorArguments)\n    {\n        var options = context.GetService<IDbContextOptions>().FindExtension<ProxiesOptionsExtension>();\n        return options == null\n            ? throw new InvalidOperationException(ProxiesStrings.ProxyServicesMissing)\n            : CreateLazyLoadingProxy(entityType, loader, constructorArguments);\n    }\n\n    private object CreateLazyLoadingProxy(\n        IEntityType entityType,\n        ILazyLoader loader,\n        object[] constructorArguments)\n        => _generator.CreateClassProxy(\n            entityType.ClrType,\n            GetInterfacesToProxy(entityType),\n            GenerationOptions,\n            constructorArguments,\n            GetNotifyChangeInterceptors(entityType, new LazyLoadingInterceptor(entityType, loader)));\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","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Proxies/Proxies/Internal/ProxyFactory.cs#L60-L96","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Call options.UseInternalServiceProvider(...).AddEntityFrameworkProxies() when using a custom internal provider, OR call UseLazyLoadingProxies() on DbContextOptionsBuilder which wires it up.","Ensure the EFCore.Proxies NuGet package is referenced.","If using AddDbContext, chain .AddEntityFrameworkProxies() or use UseLazyLoadingProxies() in the options action.","Do not share a single internal service provider across contexts unless proxies are registered for all of them."],"exampleFix":"// before\nservices.AddDbContext<MyContext>(o => o.UseSqlServer(conn));\n// later: proxy creation throws\n\n// after\nservices.AddDbContext<MyContext>(o =>\n    o.UseSqlServer(conn)\n     .UseLazyLoadingProxies());\n// or for custom provider:\noptions.UseInternalServiceProvider(sp);\nservices.AddEntityFrameworkProxies();","handlingStrategy":"validation","validationCode":"// Verify the proxies extension is configured\nvar ext = options.Options.FindExtension<ProxiesOptionsExtension>();\nif (ext == null)\n    throw new InvalidOperationException(\"Call AddEntityFrameworkProxies / UseLazyLoadingProxies first.\");","typeGuard":"static bool ProxiesRegistered(DbContextOptionsBuilder o)\n    => o.Options.FindExtension<ProxiesOptionsExtension>() is not null;","tryCatchPattern":null,"preventionTips":["Always chain UseLazyLoadingProxies/UseChangeTrackingProxies in the same AddDbContext call.","When using UseInternalServiceProvider, register AddEntityFrameworkProxies on it explicitly.","Add a startup test asserting the proxies extension is present."],"tags":["efcore","proxies","service-provider","di","lazy-loading"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}