{"record":{"id":"6eb49cdbe8ff72a2","repo":"dotnet/efcore","slug":"unable-to-create-proxy-for-entitytype-because","errorCode":null,"errorMessage":"Unable to create proxy for '{entityType}' because proxies are not enabled. Call 'DbContextOptionsBuilder.UseChangeTrackingProxies' or 'DbContextOptionsBuilder.UseLazyLoadingProxies' to enable proxies.","messagePattern":"Unable to create proxy for '(.+?)' because proxies are not enabled\\. Call 'DbContextOptionsBuilder\\.UseChangeTrackingProxies' or 'DbContextOptionsBuilder\\.UseLazyLoadingProxies' to enable proxies\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Proxies/ProxiesExtensions.cs","lineNumber":332,"sourceCode":"        this IServiceProvider serviceProvider,\n        Type entityType,\n        params object[] constructorArguments)\n    {\n        CheckProxyOptions(serviceProvider, entityType.ShortDisplayName());\n\n        return serviceProvider.GetRequiredService<IProxyFactory>().Create(\n            serviceProvider.GetRequiredService<ICurrentDbContext>().Context,\n            entityType,\n            constructorArguments);\n    }\n\n    private static void CheckProxyOptions(IServiceProvider serviceProvider, string entityTypeName)\n    {\n        var options = serviceProvider.GetRequiredService<IDbContextOptions>().FindExtension<ProxiesOptionsExtension>();\n\n        if (options?.UseProxies != true)\n        {\n            throw new InvalidOperationException(ProxiesStrings.ProxiesNotEnabled(entityTypeName));\n        }\n    }\n}\n","sourceCodeStart":314,"sourceCodeEnd":336,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Proxies/ProxiesExtensions.cs#L314-L336","documentation":"Thrown by ProxiesExtensions.CheckProxyOptions when a CreateProxy extension is called but the resolved IDbContextOptions has no ProxiesOptionsExtension with UseProxies == true. This is the user-facing guard: proxies must be explicitly turned on via UseChangeTrackingProxies or UseLazyLoadingProxies before any proxy can be created.","triggerScenarios":"Calling the public CreateProxy<T>() / CreateProxy(entityType) extension methods on a context (or service provider) whose options never enabled proxies (options?.UseProxies != true).","commonSituations":"Forgot the Use*Proxies() call in AddDbContext/Configure; enabled proxies behind a flag that evaluated false in the current environment; calling CreateProxy on a context constructed with a different options object than the one configured for proxies.","solutions":["Call optionsBuilder.UseChangeTrackingProxies() or optionsBuilder.UseLazyLoadingProxies() when configuring the DbContext.","Make sure the options are applied to the same context/service provider from which CreateProxy is resolved.","If using the internal service provider pattern, also call AddEntityFrameworkProxies()."],"exampleFix":"// before\nvar options = new DbContextOptionsBuilder<BloggingContext>()\n    .UseSqlServer(connStr).Options;\nusing var ctx = new BloggingContext(options);\nvar p = ctx.CreateProxy<Blog>(); // throws ProxiesNotEnabled\n\n// after\nvar options = new DbContextOptionsBuilder<BloggingContext>()\n    .UseSqlServer(connStr)\n    .UseLazyLoadingProxies()\n    .Options;\nvar p = ctx.CreateProxy<Blog>();","handlingStrategy":"validation","validationCode":"// Mirror the library's own guard before calling CreateProxy\nvar options = context.GetService<IDbContextOptions>().FindExtension<ProxiesOptionsExtension>();\nif (options?.UseProxies != true)\n    throw new InvalidOperationException(\"Enable proxies via UseChangeTrackingProxies/UseLazyLoadingProxies first.\");\nvar proxy = context.CreateProxy<Order>();","typeGuard":"static bool ProxiesAreEnabled(DbContext c)\n    => c.GetService<IDbContextOptions>().FindExtension<ProxiesOptionsExtension>()?.UseProxies == true;","tryCatchPattern":null,"preventionTips":["Enable proxies (Use*Proxies) on the options used by the context that resolves CreateProxy.","Keep an integration test that creates a proxy for a known entity.","Centralize DbContext options construction in one place to avoid diverging configurations."],"tags":["ef-core","proxies","dbcontext-options","configuration"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}