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

  1. Call optionsBuilder.UseChangeTrackingProxies() or optionsBuilder.UseLazyLoadingProxies() when configuring the DbContext.
  2. Make sure the options are applied to the same context/service provider from which CreateProxy is resolved.
  3. 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

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


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)