{"record":{"id":"d5743d645dab1d95","repo":"dotnet/efcore","slug":"model-building-is-not-supported-when-publishing-wi-d5743d","errorCode":null,"errorMessage":"Model building is not supported when publishing with NativeAOT. Use a compiled model.","messagePattern":"Model building is not supported when publishing with NativeAOT\\. Use a compiled model\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Metadata/RuntimeDbFunction.cs","lineNumber":101,"sourceCode":"\n    /// <summary>\n    ///     Gets the name of the function in the model.\n    /// </summary>\n    public virtual string ModelName { get; }\n\n    /// <summary>\n    ///     Gets or sets the type mapping for the function's return type.\n    /// </summary>\n    /// <returns>The type mapping.</returns>\n    public virtual RelationalTypeMapping? TypeMapping\n    {\n        get => _isScalar\n            ? NonCapturingLazyInitializer.EnsureInitialized(\n                ref _typeMapping, this, static dbFunction =>\n                {\n                    if (!RuntimeFeature.IsDynamicCodeSupported)\n                    {\n                        throw new InvalidOperationException(CoreStrings.NativeAotNoCompiledModel);\n                    }\n\n                    var relationalTypeMappingSource =\n                        (IRelationalTypeMappingSource)((IModel)dbFunction.Model).GetModelDependencies().TypeMappingSource;\n                    return !string.IsNullOrEmpty(dbFunction._storeType)\n                        ? relationalTypeMappingSource.FindMapping(dbFunction._returnType, dbFunction._storeType)!\n                        : relationalTypeMappingSource.FindMapping(dbFunction._returnType, dbFunction.Model)!;\n                })\n            : _typeMapping;\n        set => _typeMapping = value;\n    }\n\n    /// <summary>\n    ///     Adds a parameter to the function.\n    /// </summary>\n    /// <param name=\"name\">The parameter name.</param>\n    /// <param name=\"clrType\">The parameter type.</param>\n    /// <param name=\"propagatesNullability\">A value which indicates whether the parameter propagates nullability.</param>","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Metadata/RuntimeDbFunction.cs#L83-L119","documentation":"The runtime DbFunction cannot resolve its return-type mapping on demand under NativeAOT because that requires the type-mapping source (dynamic code). The TypeMapping getter throws CoreStrings.NativeAotNoCompiledModel when RuntimeFeature.IsDynamicCodeSupported is false.","triggerScenarios":"Accessing RuntimeDbFunction.TypeMapping in a NativeAOT-published app without a compiled model, where the type mapping was not precomputed and stored in _typeMapping.","commonSituations":"Publishing with <PublishAot>true</PublishAot> and querying a DbFunction at runtime; compiled model not generated or not wired into DbContextOptions; relying on runtime type-mapping discovery in an AOT binary.","solutions":["Generate a compiled model via 'dotnet ef dbcontext optimize' so DbFunction type mappings are baked in.","Register the compiled model on DbContextOptions at startup.","Avoid accessing TypeMapping on RuntimeDbFunction in app code; it is runtime infrastructure.","Confirm the publish profile sets up the compiled model reference."],"exampleFix":"// before\n// AOT publish, no compiled model -> TypeMapping getter throws\n\n// after\ndotnet ef dbcontext optimize\n// then in DbContext configuration:\noptions.UseModel(CompiledModels.MyContext.Instance);","handlingStrategy":"validation","validationCode":"if (RuntimeFeature.IsDynamicCodeSupported)\n{\n    _ = dbFunction.TypeMapping;\n}\nelse\n{\n    throw new InvalidOperationException(\n        \"Compiled model required for DbFunction type mapping under NativeAOT.\");\n}","typeGuard":"static bool CanResolveTypeMappingAtRuntime()\n    => RuntimeFeature.IsDynamicCodeSupported;","tryCatchPattern":null,"preventionTips":["Generate and reference a compiled model for NativeAOT deployments.","Avoid reading TypeMapping on RuntimeDbFunction in app code.","Verify the compiled model is wired into DbContextOptions at startup."],"tags":["ef-core","native-aot","compiled-model","dbfunction","type-mapping"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}