{"record":{"id":"7c9f6933c4c16abc","repo":"dotnet/efcore","slug":"unable-to-translate-type-type","errorCode":null,"errorMessage":"Unable to translate type '{type}'.","messagePattern":"Unable to translate type '(.+?)'\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":1314,"sourceCode":"        if (labelTarget.Type != typeof(void))\n        {\n            throw new NotImplementedException(\"Non-void label target\");\n        }\n\n        // We did a processing pass on the block's labels, so any labels should already be found in our label stack frame\n        return IdentifierName(_stack.Peek().Labels[labelTarget]);\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    protected virtual TypeSyntax Generate(Type type)\n        => TryGenerate(type, out var result)\n            ? result\n            : throw new NotSupportedException(DesignStrings.UnableToTranslateType(type.DisplayName(fullName: false)));\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    protected virtual bool TryGenerate(Type type, [NotNullWhen(true)] out TypeSyntax? result)\n    {\n        result = null;\n        if (type.IsAnonymousType())\n        {\n            return false;\n        }\n\n        if (type.IsGenericType)\n        {\n            if (type.IsConstructedGenericType","sourceCodeStart":1296,"sourceCodeEnd":1332,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L1296-L1332","documentation":"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#.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Replace anonymous types with named DTO classes when they appear in unsupported positions.","Avoid ref/pointer types in precompiled-query expressions.","Inspect the type named in the error message and restructure the query to use a type with a straightforward C# representation."],"exampleFix":"// before: anonymous type used where Generate is reached\nvar q = ctx.Set<T>().Select(x => new { x.Id }).OrderBy(a => a.Id);\n// after: named DTO\npublic sealed record IdProj(int Id);\nvar q = ctx.Set<T>().Select(x => new IdProj(x.Id)).OrderBy(a => a.Id);","handlingStrategy":"validation","validationCode":"// Check that types used in the tree are nameable (not anonymous, not byref/pointer)\npublic sealed class TypeDetector : ExpressionVisitor {\n    public bool Found;\n    protected override Expression VisitMember(MemberExpression m) { Check(m.Type); return m; }\n    void Check(Type t) { if (t.IsAnonymousType() || t.IsByRefLike || t.IsPointer) Found = true; }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use named DTO classes instead of anonymous types where the translator reaches Generate.","Avoid ref/pointer types in precompiled-query expressions.","Inspect the type named in the error and restructure to a nameable type."],"tags":["ef-core","linq","expression-tree","type-system","code-generation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}