dotnet/efcore · error · InvalidOperationException
Model building is not supported when publishing with…
Error message
Model building is not supported when publishing with NativeAOT. Use a compiled model.
What it means
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.
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.
Example fix
// before // AOT publish, no compiled model -> TypeMapping getter throws // after dotnet ef dbcontext optimize // then in DbContext configuration: options.UseModel(CompiledModels.MyContext.Instance);
Defensive patterns
Strategy: validation
Validate before calling
if (RuntimeFeature.IsDynamicCodeSupported)
{
_ = dbFunction.TypeMapping;
}
else
{
throw new InvalidOperationException(
"Compiled model required for DbFunction type mapping under NativeAOT.");
} Type guard
static bool CanResolveTypeMappingAtRuntime()
=> RuntimeFeature.IsDynamicCodeSupported; Prevention
- 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.
When it happens
Trigger: Accessing RuntimeDbFunction.TypeMapping in a NativeAOT-published app without a compiled model, where the type mapping was not precomputed and stored in _typeMapping.
Common situations: 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.
Related errors
- Model building is not supported when publishing with…
- Model building is not supported when publishing with…
- The function ' ' has a custom translation. A compiled model…
- The DbFunction ' ' has an invalid return type ' '. Ensure…
- The parameter ' ' for the DbFunction ' ' has an invalid…
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/d5743d645dab1d95.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Relational/Metadata/RuntimeDbFunction.cs:101
/// <summary>
/// Gets the name of the function in the model.
/// </summary>
public virtual string ModelName { get; }
/// <summary>
/// Gets or sets the type mapping for the function's return type.
/// </summary>
/// <returns>The type mapping.</returns>
public virtual RelationalTypeMapping? TypeMapping
{
get => _isScalar
? NonCapturingLazyInitializer.EnsureInitialized(
ref _typeMapping, this, static dbFunction =>
{
if (!RuntimeFeature.IsDynamicCodeSupported)
{
throw new InvalidOperationException(CoreStrings.NativeAotNoCompiledModel);
}
var relationalTypeMappingSource =
(IRelationalTypeMappingSource)((IModel)dbFunction.Model).GetModelDependencies().TypeMappingSource;
return !string.IsNullOrEmpty(dbFunction._storeType)
? relationalTypeMappingSource.FindMapping(dbFunction._returnType, dbFunction._storeType)!
: relationalTypeMappingSource.FindMapping(dbFunction._returnType, dbFunction.Model)!;
})
: _typeMapping;
set => _typeMapping = value;
}
/// <summary>
/// Adds a parameter to the function.
/// </summary>
/// <param name="name">The parameter name.</param>
/// <param name="clrType">The parameter type.</param>
/// <param name="propagatesNullability">A value which indicates whether the parameter propagates nullability.</param>View on GitHub (pinned to 3a2006ef56)