{"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":"critical","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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Metadata/RuntimeDbFunctionParameter.cs#L55-L91","documentation":"Thrown when EF Core lazily resolves the relational type mapping for a runtime DbFunction parameter (RuntimeDbFunctionParameter.TypeMapping) under NativeAOT, where dynamic code is unsupported. Like the DbFunction return mapping, parameter mapping resolution requires the runtime type-mapping source and cannot run under AOT without a compiled model.","triggerScenarios":"Querying through a mapped DbFunction that takes parameters, in an app published with `<PublishAot>true</PublishAot>` and no compiled model; reading `.TypeMapping` on a parameter when `RuntimeFeature.IsDynamicCodeSupported` is false.","commonSituations":"AOT-published app that uses parameterized DbFunctions; missing or stale compiled model after adding parameters to a DbFunction; works in dev (JIT) but throws on `dotnet publish` AOT.","solutions":["Generate a compiled model via `dotnet ef dbcontext optimize` and register it with `UseCompiledModel`.","Re-run the optimizer whenever DbFunction signatures (parameters) change.","Drop `<PublishAot>` if you cannot use a compiled model.","Validate the compiled model is active at startup (assert `RuntimeFeature.IsDynamicCodeSupported` or that the compiled model type is registered)."],"exampleFix":"// before\noptions.UseSqlServer(cs);\n// after\ndotnet ef dbcontext optimize\noptions.UseSqlServer(cs).UseCompiledModel(typeof(MyCompiledModel).Assembly);","handlingStrategy":"validation","validationCode":"if (!System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported\n    && options.FindExtension<CoreOptionsExtension>()?.CompiledModel is null)\n{\n    throw new InvalidOperationException(\"DbFunction parameter type mapping requires a compiled model under NativeAOT.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["For AOT, always ship a compiled model that covers DbFunction parameters.","Re-run `dotnet ef dbcontext optimize` when DbFunction signatures change.","Validate `RuntimeFeature.IsDynamicCodeSupported` in integration tests of the published app."],"tags":["native-aot","efcore","compiled-model","dbfunction","type-mapping","relational"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}