{"record":{"id":"4e1d6b4061e750a4","repo":"dotnet/efcore","slug":"model-building-is-not-supported-when-publishing-wi-4e1d6b","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":"error","filePath":"src/EFCore.Relational/Metadata/Internal/UniqueConstraint.cs","lineNumber":93,"sourceCode":"    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual void SetRowKeyValueFactory(IRowKeyValueFactory factory)\n        => _rowKeyValueFactory = factory;\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual IRowKeyValueFactory GetRowKeyValueFactory()\n        => NonCapturingLazyInitializer.EnsureInitialized(\n            ref _rowKeyValueFactory, this,\n            static constraint =>\n                RuntimeFeature.IsDynamicCodeSupported\n                    ? constraint.Table.Model.Model.GetRelationalDependencies().RowKeyValueFactoryFactory.Create(constraint)\n                    : throw new InvalidOperationException(CoreStrings.NativeAotNoCompiledModel));\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public override string ToString()\n        => ((IUniqueConstraint)this).ToDebugString(MetadataDebugStringOptions.SingleLineDefault);\n\n    /// <inheritdoc />\n    ITable IUniqueConstraint.Table\n        => Table;\n\n    /// <inheritdoc />\n    IReadOnlyList<IColumn> IUniqueConstraint.Columns\n        => Columns;\n","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Metadata/Internal/UniqueConstraint.cs#L75-L111","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Generate and reference a compiled model with 'dotnet ef dbcontext optimize' so row-key factories are baked in.","Set RuntimeFeature.IsDynamicCodeSupported expectations correctly: under AOT, only compiled-model code paths are supported.","Avoid calling GetRowKeyValueFactory directly in application code; it is internal infrastructure.","Ensure your publish profile includes the compiled-model reference and the model is loaded at startup."],"exampleFix":"// before\n// publishes with AOT, no compiled model\n// -> GetRowKeyValueFactory throws at runtime\n\n// after\n// 1) generate compiled model\ndotnet ef dbcontext optimize\n// 2) register the compiled model on DbContextOptions\noptions.UseModel(MyCompiledModel.Instance);","handlingStrategy":"validation","validationCode":"if (RuntimeFeature.IsDynamicCodeSupported\n    || HasCompiledModel(options))\n{\n    constraint.GetRowKeyValueFactory();\n}","typeGuard":"static bool SupportsOnDemandModelBuilding()\n    => RuntimeFeature.IsDynamicCodeSupported;","tryCatchPattern":null,"preventionTips":["Generate a compiled model before publishing with NativeAOT.","Do not call GetRowKeyValueFactory from application code; it is internal infrastructure.","Validate the AOT publish pipeline includes 'dotnet ef dbcontext optimize'."],"tags":["ef-core","native-aot","compiled-model","runtime","metadata"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}