{"record":{"id":"a4fbb4464ef8a2f8","repo":"dotnet/efcore","slug":"the-function-function-has-a-custom-translation","errorCode":null,"errorMessage":"The function '{function}' has a custom translation. A compiled model cannot be generated because custom function translations are not supported.","messagePattern":"The function '(.+?)' has a custom translation\\. A compiled model cannot be generated because custom function translations are not supported\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Design/Internal/RelationalCSharpRuntimeAnnotationCodeGenerator.cs","lineNumber":1832,"sourceCode":"                    .Append(tableVariable).Append($\".AddRowInternalForeignKey({structuralTypeVariable}, \")\n                    .AppendLine(\"RelationalModel.GetForeignKey(this,\").IncrementIndent()\n                    .AppendLine($\"{code.Literal(internalForeignKey.DeclaringEntityType.Name)},\")\n                    .AppendLine($\"{code.Literal(internalForeignKey.Properties.Select(p => p.Name).ToArray())},\")\n                    .AppendLine($\"{code.Literal(internalForeignKey.PrincipalEntityType.Name)},\")\n                    .AppendLine($\"{code.Literal(internalForeignKey.PrincipalKey.Properties.Select(p => p.Name).ToArray())}));\")\n                    .DecrementIndent();\n            }\n        }\n    }\n\n    private void Create(\n        IDbFunction function,\n        string functionsVariable,\n        CSharpRuntimeAnnotationCodeGeneratorParameters parameters)\n    {\n        if (function.Translation != null)\n        {\n            throw new InvalidOperationException(RelationalStrings.CompiledModelFunctionTranslation(function.Name));\n        }\n\n        AddNamespace(function.ReturnType, parameters.Namespaces);\n\n        var code = Dependencies.CSharpHelper;\n        var functionVariable = code.Identifier(\n            function.MethodInfo?.Name ?? function.Name, function, parameters.ScopeObjects, capitalize: false);\n        var mainBuilder = parameters.MainBuilder;\n        mainBuilder\n            .Append(\"var \").Append(functionVariable).AppendLine(\" = new RuntimeDbFunction(\").IncrementIndent()\n            .Append(code.Literal(function.ModelName)).AppendLine(\",\")\n            .Append(parameters.TargetName).AppendLine(\",\")\n            .Append(code.Literal(function.ReturnType)).AppendLine(\",\")\n            .Append(code.Literal(function.Name));\n\n        if (function.Schema != null)\n        {\n            mainBuilder.AppendLine(\",\")","sourceCodeStart":1814,"sourceCodeEnd":1850,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Design/Internal/RelationalCSharpRuntimeAnnotationCodeGenerator.cs#L1814-L1850","documentation":"Thrown by RelationalCSharpRuntimeAnnotationCodeGenerator when building a compiled model if a DbFunction has a custom Translation delegate (function.Translation != null). Custom translations are runtime delegates that cannot be serialized into the precompiled C# model, so compiled-model generation is blocked.","triggerScenarios":"Running 'dotnet ef dbcontext optimize' / DesignTimeService compiled-model generation for a context that registers a DbFunction with HasTranslation(...) or a custom IDbMethodTranslator producing a Translation delegate (RelationalCSharpRuntimeAnnotationCodeGenerator.cs:1830-1832).","commonSituations":"Registering user-defined function mappings with custom SQL translation; using HasTranslation for complex DB function mappings; then enabling compiled models for startup performance.","solutions":["Remove the custom translation for the function (drop HasTranslation / the Translation delegate) before generating a compiled model.","Map the function via a store function or built-in translation that the compiled model can serialize.","Skip compiled-model generation for this context and rely on the runtime model.","If the translation is essential, keep it but do not enable compiled models."],"exampleFix":"// before\nmodelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(MyUdf))!)\n    .HasTranslation(args => /* custom SqlExpression */);\n// dotnet ef dbcontext optimize  -> throws\n\n// after (remove custom translation to allow compiled model)\nmodelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(MyUdf))!);\n// map via schema/store name instead","handlingStrategy":"validation","validationCode":"// Before compiled-model generation, check for custom translations\nvar hasCustom = context.Model.GetDbFunctions().Any(f => f.Translation != null);\nif (hasCustom)\n    throw new InvalidOperationException(\n        \"Remove HasTranslation/Translation delegates before compiling the model.\");","typeGuard":"static bool HasCustomTranslations(IModel model)\n    => model.GetDbFunctions().Any(f => f.Translation is not null);","tryCatchPattern":null,"preventionTips":["Avoid HasTranslation if you intend to use compiled models.","Prefer store-function mappings the compiled model can serialize.","Run a CI check that fails compiled-model generation early when translations are present."],"tags":["efcore","compiled-model","dbfunction","design-time","optimization"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}