dotnet/efcore · error · InvalidOperationException

The entity type '{entityType}' was not found. Ensure that th

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 FindRuntimeEntityType(type) returns null and the type is not a shared type. This means the CLR type passed in is not part of the EF model at all, so no proxy can be created for it.

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

Solutions

  1. Register the type in the model (modelBuilder.Entity<T>() or via DbSet<T> property) so FindRuntimeEntityType resolves it.
  2. Pass the concrete entity Type rather than a base class, interface, or DTO.
  3. If the type should not be an entity, stop using the proxy factory for it — proxies are only for tracked entity types.
  4. Check context.Model.GetEntityTypes() to confirm which types are actually mapped.

Example fix

// before
var proxy = context.CreateProxy(typeof(CustomerDto), args); // CustomerDto not in model

// after
// register the real entity
public DbSet<Customer> Customers { get; set; }
var proxy = context.CreateProxy(typeof(Customer), args);
Defensive patterns

Strategy: validation

Validate before calling

// Verify the type is mapped before proxy creation
var et = context.Model.FindRuntimeEntityType(type);
if (et == null)
    throw new InvalidOperationException($"{type} is not in the model. Register via DbSet/Entity<T>().");

Type guard

static bool IsMappedEntity(DbContext context, Type t)
    => context.Model.FindRuntimeEntityType(t) is not null;

Prevention

When it happens

Trigger: Calling ProxyFactory.Create / DbSet-based proxy creation with a Type that has not been registered as an entity in the model (ProxyFactory.cs:37-45). Happens when passing a base class, a DTO, a non-entity class, or a type that is only referenced via an owned/complex type.

Common situations: Passing a base type or interface whose concrete subtype is the actual entity; forgetting to register an entity in OnModelCreating; using a DTO/view-model type with the proxy API; namespace collisions where the wrong Type is resolved.

Related errors


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