{"record":{"id":"2037dabc7601d28d","repo":"dotnet/efcore","slug":"model-building-is-not-supported-when-publishing-wi-2037da","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/RuntimeDbFunctionParameter.cs","lineNumber":73,"sourceCode":"    }\n\n    /// <summary>\n    ///     Gets the function to which this parameter belongs.\n    /// </summary>\n    public virtual RuntimeDbFunction Function { get; }\n\n    /// <summary>\n    ///     Gets or sets the type mapping for this parameter.\n    /// </summary>\n    /// <returns>The type mapping.</returns>\n    public virtual RelationalTypeMapping? TypeMapping\n    {\n        get => NonCapturingLazyInitializer.EnsureInitialized(\n            ref _typeMapping, this, static parameter =>\n            {\n                if (!RuntimeFeature.IsDynamicCodeSupported)\n                {\n                    throw new InvalidOperationException(CoreStrings.NativeAotNoCompiledModel);\n                }\n\n                var relationalTypeMappingSource =\n                    (IRelationalTypeMappingSource)((IModel)parameter.Function.Model).GetModelDependencies().TypeMappingSource;\n                return relationalTypeMappingSource.FindMapping(parameter._storeType)!;\n            });\n\n        set => _typeMapping = value;\n    }\n\n    /// <summary>\n    ///     Returns a string that represents the current object.\n    /// </summary>\n    /// <returns>A string that represents the current object.</returns>\n    public override string ToString()\n        => ((IDbFunctionParameter)this).ToDebugString(MetadataDebugStringOptions.SingleLineDefault);\n\n    /// <summary>","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Metadata/RuntimeDbFunctionParameter.cs#L55-L91","documentation":"The runtime DbFunction parameter cannot resolve its type mapping on demand under NativeAOT, because doing so requires the relational type-mapping source (dynamic code). The TypeMapping getter throws CoreStrings.NativeAotNoCompiledModel when dynamic code is unsupported.","triggerScenarios":"Accessing RuntimeDbFunctionParameter.TypeMapping in a NativeAOT-published app where the mapping was not precomputed into _typeMapping.","commonSituations":"AOT publishing without a compiled model; querying DbFunction parameters' mappings at runtime; compiled model generated but not referenced on DbContextOptions.","solutions":["Generate and reference a compiled model so parameter type mappings are precomputed.","Run 'dotnet ef dbcontext optimize' and wire the compiled model into options.","Do not read TypeMapping on RuntimeDbFunctionParameter from application code.","Verify the AOT publish profile includes the compiled-model assembly."],"exampleFix":"// before\n// AOT publish -> RuntimeDbFunctionParameter.TypeMapping throws\n\n// after\ndotnet ef dbcontext optimize\noptions.UseModel(CompiledModels.MyContext.Instance);","handlingStrategy":"validation","validationCode":"if (RuntimeFeature.IsDynamicCodeSupported)\n{\n    _ = parameter.TypeMapping;\n}","typeGuard":"static bool CanResolveTypeMappingAtRuntime()\n    => RuntimeFeature.IsDynamicCodeSupported;","tryCatchPattern":null,"preventionTips":["Pair every NativeAOT publish with a compiled model.","Do not access TypeMapping on RuntimeDbFunctionParameter from app code.","Confirm the compiled-model reference is present in the published binary."],"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"}