dotnet/efcore · error · InvalidOperationException

The entity type ' ' was not found. Ensure that the entity…

Error message

The entity type '{entityType}' was not found. Ensure that the entity type has been added to the model.

What it means

Thrown by ProxyFactory.Create when the supplied CLR type is not part of the model at all: FindRuntimeEntityType(type) is null and the type is not registered as shared. This reuses CoreStrings.EntityTypeNotFound. The type was never added to the model via DbSet, OnModelCreating, or Entity<T>().

Solutions

  1. Add the type to the model: declare a DbSet<T> or call modelBuilder.Entity<T>() in OnModelCreating.
  2. Verify the CLR type passed to CreateProxy exactly matches the registered entity type.
  3. Ensure the DbContext the proxy factory is resolving against is the one that owns the entity type.

Example fix

// before: OrderDetail is not in the model
var proxy = context.CreateProxy<OrderDetail>();

// after: register the entity type
public DbSet<OrderDetail> OrderDetails { get; set; }
// or in OnModelCreating:
modelBuilder.Entity<OrderDetail>();
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the type is part of the model before creating a proxy
var et = context.Model.FindEntityType(typeof(OrderDetail));
if (et == null)
    throw new InvalidOperationException(typeof(OrderDetail).Name + " is not in the model.");
var proxy = context.CreateProxy<OrderDetail>();

Type guard

static bool IsRegisteredEntity(IModel model, Type t) => model.FindEntityType(t) != null;

Prevention

When it happens

Trigger: Calling CreateProxy<T> for a type that has no corresponding entity type in the context's model (not declared in any DbSet, not configured in OnModelCreating).

Common situations: Passing a DTO or view-model type to CreateProxy by mistake; typo in the generic argument; calling CreateProxy before the model is built/seeded; using a type from another assembly that was never registered.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/6b180703cb732ed6. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Proxies/Proxies/Internal/ProxyFactory.cs:45

    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual object Create(
        DbContext context,
        Type type,
        params object[] constructorArguments)
    {
        var entityType = context.Model.FindRuntimeEntityType(type);
        if (entityType == null)
        {
            if (context.Model.IsShared(type))
            {
                throw new InvalidOperationException(ProxiesStrings.EntityTypeNotFoundShared(type.ShortDisplayName()));
            }

            throw new InvalidOperationException(CoreStrings.EntityTypeNotFound(type.ShortDisplayName()));
        }

        return CreateProxy(context, entityType, constructorArguments);
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual Type CreateProxyType(
        IEntityType entityType)
        => _generator.ProxyBuilder.CreateClassProxyType(
            entityType.ClrType,
            GetInterfacesToProxy(entityType),
            GenerationOptions);

View on GitHub (pinned to 3a2006ef56)