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 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.

Solutions

  1. Generate and reference a compiled model so parameter type mappings are precomputed.
  2. Run 'dotnet ef dbcontext optimize' and wire the compiled model into options.
  3. Do not read TypeMapping on RuntimeDbFunctionParameter from application code.
  4. Verify the AOT publish profile includes the compiled-model assembly.

Example fix

// before
// AOT publish -> RuntimeDbFunctionParameter.TypeMapping throws

// after
dotnet ef dbcontext optimize
options.UseModel(CompiledModels.MyContext.Instance);
Defensive patterns

Strategy: validation

Validate before calling

if (RuntimeFeature.IsDynamicCodeSupported)
{
    _ = parameter.TypeMapping;
}

Type guard

static bool CanResolveTypeMappingAtRuntime()
    => RuntimeFeature.IsDynamicCodeSupported;

Prevention

When it happens

Trigger: Accessing RuntimeDbFunctionParameter.TypeMapping in a NativeAOT-published app where the mapping was not precomputed into _typeMapping.

Common situations: AOT publishing without a compiled model; querying DbFunction parameters' mappings at runtime; compiled model generated but not referenced on DbContextOptions.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/2037dabc7601d28d. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Relational/Metadata/RuntimeDbFunctionParameter.cs:73

    }

    /// <summary>
    ///     Gets the function to which this parameter belongs.
    /// </summary>
    public virtual RuntimeDbFunction Function { get; }

    /// <summary>
    ///     Gets or sets the type mapping for this parameter.
    /// </summary>
    /// <returns>The type mapping.</returns>
    public virtual RelationalTypeMapping? TypeMapping
    {
        get => NonCapturingLazyInitializer.EnsureInitialized(
            ref _typeMapping, this, static parameter =>
            {
                if (!RuntimeFeature.IsDynamicCodeSupported)
                {
                    throw new InvalidOperationException(CoreStrings.NativeAotNoCompiledModel);
                }

                var relationalTypeMappingSource =
                    (IRelationalTypeMappingSource)((IModel)parameter.Function.Model).GetModelDependencies().TypeMappingSource;
                return relationalTypeMappingSource.FindMapping(parameter._storeType)!;
            });

        set => _typeMapping = value;
    }

    /// <summary>
    ///     Returns a string that represents the current object.
    /// </summary>
    /// <returns>A string that represents the current object.</returns>
    public override string ToString()
        => ((IDbFunctionParameter)this).ToDebugString(MetadataDebugStringOptions.SingleLineDefault);

    /// <summary>

View on GitHub (pinned to 3a2006ef56)