dotnet/efcore · error · InvalidOperationException

Unable to create proxy for '{entityType}' because proxies ar

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 options?.UseProxies != true. This guards the CreateProxy extension methods on IServiceProvider/DbSet, ensuring proxies were actually enabled before constructing one.

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 dbf9771522)

Solutions

  1. Add options.UseLazyLoadingProxies() or options.UseChangeTrackingProxies() to OnConfiguring / AddDbContext.
  2. Ensure the options builder being configured is the same one the context actually uses (no stray builder instances).
  3. Confirm EFCore.Proxies is referenced; the Use* methods are extension methods that won't even compile otherwise.

Example fix

// before
services.AddDbContext<MyContext>(o => o.UseSqlServer(conn));
var proxy = context.Set<Order>().CreateProxy(); // throws

// after
services.AddDbContext<MyContext>(o =>
    o.UseSqlServer(conn).UseLazyLoadingProxies());
var proxy = context.Set<Order>().CreateProxy();
Defensive patterns

Strategy: validation

Validate before calling

var ext = options.FindExtension<ProxiesOptionsExtension>();
if (ext?.UseProxies != true)
    throw new InvalidOperationException("Call UseChangeTrackingProxies/UseLazyLoadingProxies before proxy APIs.");

Type guard

static bool ProxiesEnabled(DbContextOptionsBuilder o)
    => o.Options.FindExtension<ProxiesOptionsExtension>()?.UseProxies == true;

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/6eb49cdbe8ff72a2. Report an issue: GitHub.