dotnet/efcore · error · NotSupportedException

Unable to translate type '{type}'.

Error message

Unable to translate type '{type}'.

What it means

Generate(Type) throws NotSupportedException with DesignStrings.UnableToTranslateType when TryGenerate returns false — i.e. the translator cannot produce a C# TypeSyntax for a type referenced in the tree. TryGenerate deliberately excludes anonymous types (handled elsewhere) and any type it cannot render as valid C#.

Source

Thrown at src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs:1314

        if (labelTarget.Type != typeof(void))
        {
            throw new NotImplementedException("Non-void label target");
        }

        // We did a processing pass on the block's labels, so any labels should already be found in our label stack frame
        return IdentifierName(_stack.Peek().Labels[labelTarget]);
    }

    /// <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>
    protected virtual TypeSyntax Generate(Type type)
        => TryGenerate(type, out var result)
            ? result
            : throw new NotSupportedException(DesignStrings.UnableToTranslateType(type.DisplayName(fullName: false)));

    /// <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>
    protected virtual bool TryGenerate(Type type, [NotNullWhen(true)] out TypeSyntax? result)
    {
        result = null;
        if (type.IsAnonymousType())
        {
            return false;
        }

        if (type.IsGenericType)
        {
            if (type.IsConstructedGenericType

View on GitHub (pinned to dbf9771522)

Solutions

  1. Replace anonymous types with named DTO classes when they appear in unsupported positions.
  2. Avoid ref/pointer types in precompiled-query expressions.
  3. Inspect the type named in the error message and restructure the query to use a type with a straightforward C# representation.

Example fix

// before: anonymous type used where Generate is reached
var q = ctx.Set<T>().Select(x => new { x.Id }).OrderBy(a => a.Id);
// after: named DTO
public sealed record IdProj(int Id);
var q = ctx.Set<T>().Select(x => new IdProj(x.Id)).OrderBy(a => a.Id);
Defensive patterns

Strategy: validation

Validate before calling

// Check that types used in the tree are nameable (not anonymous, not byref/pointer)
public sealed class TypeDetector : ExpressionVisitor {
    public bool Found;
    protected override Expression VisitMember(MemberExpression m) { Check(m.Type); return m; }
    void Check(Type t) { if (t.IsAnonymousType() || t.IsByRefLike || t.IsPointer) Found = true; }
}

Prevention

When it happens

Trigger: The translator must reference a type it cannot name: anonymous types (returned as false by TryGenerate outside the dedicated anonymous-new path), byref/pointer types, or generic shapes that have no clean C# spelling.

Common situations: Using types that defeat the C# syntax generator (ref structs, pointers, open generics in unsupported positions) inside a precompiled query or model-building lambda; anonymous types reaching Generate via an unexpected path.

Related errors


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