{"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":"critical","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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Metadata/RuntimeDbFunction.cs#L83-L119","documentation":"Thrown when EF Core lazily resolves the relational type mapping for a runtime DbFunction's return type (RuntimeDbFunction.TypeMapping) but the app was published with NativeAOT, where `RuntimeFeature.IsDynamicCodeSupported` is false. Runtime type-mapping resolution requires dynamic code, so under AOT a compiled model is mandatory.","triggerScenarios":"Using a mapped DbFunction in a LINQ query under a NativeAOT-published app without a compiled model; any path that reads `.TypeMapping` on a RuntimeDbFunction when dynamic code is not supported.","commonSituations":"Publishing with `<PublishAot>true</PublishAot>` and referencing DbFunctions in queries; AOT app that worked in Debug (JIT) but fails when published; forgetting to regenerate the compiled model after adding a DbFunction.","solutions":["Generate and register a compiled model with `dotnet ef dbcontext optimize` + `UseCompiledModel`.","Regenerate the compiled model after adding/changing any DbFunction mapping.","Disable NativeAOT publishing if compiled models are not viable for the project.","Confirm `RuntimeFeature.IsDynamicCodeSupported` in the deployed environment to catch AOT publish misconfiguration early."],"exampleFix":"// before\nmodelBuilder.HasDbFunction(typeof(MyContext).GetMethod(nameof(MyFunc))!);\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 type mapping requires a compiled model under NativeAOT.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Generate and register a compiled model for any AOT deployment that uses DbFunctions.","Regenerate the compiled model after adding or changing DbFunction mappings.","Smoke-test mapped DbFunctions against the published (trimmed) app, not just in Debug."],"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"}