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

Under NativeAOT, dynamic code (reflection-based model building) is unavailable, so the row-key value factory cannot be created on demand. GetRowKeyValueFactory throws CoreStrings.NativeAotNoCompiledModel when RuntimeFeature.IsDynamicCodeSupported is false.

Solutions

  1. Generate and reference a compiled model with 'dotnet ef dbcontext optimize' so row-key factories are baked in.
  2. Set RuntimeFeature.IsDynamicCodeSupported expectations correctly: under AOT, only compiled-model code paths are supported.
  3. Avoid calling GetRowKeyValueFactory directly in application code; it is internal infrastructure.
  4. Ensure your publish profile includes the compiled-model reference and the model is loaded at startup.

Example fix

// before
// publishes with AOT, no compiled model
// -> GetRowKeyValueFactory throws at runtime

// after
// 1) generate compiled model
dotnet ef dbcontext optimize
// 2) register the compiled model on DbContextOptions
options.UseModel(MyCompiledModel.Instance);
Defensive patterns

Strategy: validation

Validate before calling

if (RuntimeFeature.IsDynamicCodeSupported
    || HasCompiledModel(options))
{
    constraint.GetRowKeyValueFactory();
}

Type guard

static bool SupportsOnDemandModelBuilding()
    => RuntimeFeature.IsDynamicCodeSupported;

Prevention

When it happens

Trigger: Calling UniqueConstraint.GetRowKeyValueFactory() in a NativeAOT-published app that did not supply a precompiled model. The factory is normally created via the RelationalDependencies.RowKeyValueFactoryFactory, which requires dynamic code.

Common situations: Publishing with <PublishAot>true</PublishAot> without first generating a compiled model (dotnet ef dbcontext optimize); running EF Core runtime APIs in an AOT context that expects the design-time pipeline.

Related errors


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

Appendix: source

Thrown at src/EFCore.Relational/Metadata/Internal/UniqueConstraint.cs:93

    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual void SetRowKeyValueFactory(IRowKeyValueFactory factory)
        => _rowKeyValueFactory = factory;

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual IRowKeyValueFactory GetRowKeyValueFactory()
        => NonCapturingLazyInitializer.EnsureInitialized(
            ref _rowKeyValueFactory, this,
            static constraint =>
                RuntimeFeature.IsDynamicCodeSupported
                    ? constraint.Table.Model.Model.GetRelationalDependencies().RowKeyValueFactoryFactory.Create(constraint)
                    : throw new InvalidOperationException(CoreStrings.NativeAotNoCompiledModel));

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public override string ToString()
        => ((IUniqueConstraint)this).ToDebugString(MetadataDebugStringOptions.SingleLineDefault);

    /// <inheritdoc />
    ITable IUniqueConstraint.Table
        => Table;

    /// <inheritdoc />
    IReadOnlyList<IColumn> IUniqueConstraint.Columns
        => Columns;

View on GitHub (pinned to 3a2006ef56)