{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Proxies/ProxiesExtensions.cs#L314-L336","documentation":"Thrown by ProxiesExtensions.CheckProxyOptions when options?.UseProxies != true. This guards the CreateProxy extension methods on IServiceProvider/DbSet, ensuring proxies were actually enabled before constructing one.","triggerScenarios":"Calling a proxy-creating extension (e.g. dbContext.Set<T>().CreateProxy() or context.CreateProxy<T>()) while UseChangeTrackingProxies/UseLazyLoadingProxies was never called on the options builder (ProxiesExtensions.cs:326-333).","commonSituations":"Copy-paste from a sample that omitted the Use*Proxies call; conditional configuration where the proxy line is skipped; testing a DbContext with a minimal options setup; believing proxies are on by default.","solutions":["Add options.UseLazyLoadingProxies() or options.UseChangeTrackingProxies() to OnConfiguring / AddDbContext.","Ensure the options builder being configured is the same one the context actually uses (no stray builder instances).","Confirm EFCore.Proxies is referenced; the Use* methods are extension methods that won't even compile otherwise."],"exampleFix":"// before\nservices.AddDbContext<MyContext>(o => o.UseSqlServer(conn));\nvar proxy = context.Set<Order>().CreateProxy(); // throws\n\n// after\nservices.AddDbContext<MyContext>(o =>\n    o.UseSqlServer(conn).UseLazyLoadingProxies());\nvar proxy = context.Set<Order>().CreateProxy();","handlingStrategy":"validation","validationCode":"var ext = options.FindExtension<ProxiesOptionsExtension>();\nif (ext?.UseProxies != true)\n    throw new InvalidOperationException(\"Call UseChangeTrackingProxies/UseLazyLoadingProxies before proxy APIs.\");","typeGuard":"static bool ProxiesEnabled(DbContextOptionsBuilder o)\n    => o.Options.FindExtension<ProxiesOptionsExtension>()?.UseProxies == true;","tryCatchPattern":null,"preventionTips":["Treat proxy enabling as a required step and assert it in tests.","Avoid conditional/proxy branching in options configuration.","Document in OnConfiguring which proxy mode is active."],"tags":["efcore","proxies","not-enabled","configuration"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}