dotnet/efcore · error · InvalidOperationException
Unable to create proxy for
Error message
Unable to create proxy for '{entityType}' because proxies are not enabled. Call 'DbContextOptionsBuilder.UseChangeTrackingProxies' or 'DbContextOptionsBuilder.UseLazyLoadingProxies' to enable proxies. What it means
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.
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().
Example fix
// before
var options = new DbContextOptionsBuilder<BloggingContext>()
.UseSqlServer(connStr).Options;
using var ctx = new BloggingContext(options);
var p = ctx.CreateProxy<Blog>(); // throws ProxiesNotEnabled
// after
var options = new DbContextOptionsBuilder<BloggingContext>()
.UseSqlServer(connStr)
.UseLazyLoadingProxies()
.Options;
var p = ctx.CreateProxy<Blog>(); Defensive patterns
Strategy: validation
Validate before calling
// Mirror the library's own guard before calling CreateProxy
var options = context.GetService<IDbContextOptions>().FindExtension<ProxiesOptionsExtension>();
if (options?.UseProxies != true)
throw new InvalidOperationException("Enable proxies via UseChangeTrackingProxies/UseLazyLoadingProxies first.");
var proxy = context.CreateProxy<Order>(); Type guard
static bool ProxiesAreEnabled(DbContext c)
=> c.GetService<IDbContextOptions>().FindExtension<ProxiesOptionsExtension>()?.UseProxies == true; Prevention
- 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.
When it happens
Trigger: Calling the public CreateProxy<T>() / CreateProxy(entityType) extension methods on a context (or service provider) whose options never enabled proxies (options?.UseProxies != true).
Common situations: 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.
Related errors
- 'UseChangeTrackingProxies' and 'UseLazyLoadingProxies' each…
- The entity type ' ' was not found. Ensure that the entity…
- The type ' ' is configured as a shared-type entity type…
- Cosmos-specific methods can only be used when the context…
- ' ' cannot be configured for the parameter ' ' of the…
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/6eb49cdbe8ff72a2.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Proxies/ProxiesExtensions.cs:332
this IServiceProvider serviceProvider,
Type entityType,
params object[] constructorArguments)
{
CheckProxyOptions(serviceProvider, entityType.ShortDisplayName());
return serviceProvider.GetRequiredService<IProxyFactory>().Create(
serviceProvider.GetRequiredService<ICurrentDbContext>().Context,
entityType,
constructorArguments);
}
private static void CheckProxyOptions(IServiceProvider serviceProvider, string entityTypeName)
{
var options = serviceProvider.GetRequiredService<IDbContextOptions>().FindExtension<ProxiesOptionsExtension>();
if (options?.UseProxies != true)
{
throw new InvalidOperationException(ProxiesStrings.ProxiesNotEnabled(entityTypeName));
}
}
}
View on GitHub (pinned to 3a2006ef56)