{"record":{"id":"d37d629888c1d501","repo":"dotnet/efcore","slug":"model-building-is-not-supported-when-publishing-wi","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/Column.cs","lineNumber":58,"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 new virtual Table Table\n        => (Table)base.Table;\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 ColumnAccessors Accessors\n    {\n        get => NonCapturingLazyInitializer.EnsureInitialized(\n            ref _accessors, this, static column =>\n                RuntimeFeature.IsDynamicCodeSupported\n                    ? ColumnAccessorsFactory.Create(column)\n                    : throw new InvalidOperationException(CoreStrings.NativeAotNoCompiledModel));\n        set => _accessors = value;\n    }\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        => ((IColumn)this).ToDebugString(MetadataDebugStringOptions.SingleLineDefault);\n\n    /// <inheritdoc />\n    ITable IColumn.Table\n    {\n        [DebuggerStepThrough]\n        get => Table;\n    }","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Metadata/Internal/Column.cs#L40-L76","documentation":"Under NativeAOT (RuntimeFeature.IsDynamicCodeSupported is false), EF Core cannot use reflection to build the per-column accessor delegates at runtime. The Column.Accessors getter throws to tell you to use a pre-built compiled model instead of runtime model building. This is enforced at every place EF would otherwise emit reflection-based code.","triggerScenarios":"Publishing an app with <PublishAot>true</PublishAot> while constructing the DbContext with new DbContext(options) (runtime model building) instead of a generated CompiledModel. The exception surfaces the first time EF needs column accessors.","commonSituations":"Enabling NativeAOT late in a project's lifecycle without regenerating the compiled model; CI pipeline shipping AOT builds for the first time; upgrading EF Core and assuming the previous runtime model still works under AOT.","solutions":["Generate a compiled model: run 'dotnet ef dbcontext optimize' (or Microsoft.EntityFrameworkCore.Design's optimized-model scaffold) and wire the generated DbContextModel into your options via UseModel(MyCompiledModel.Model).","Confirm <PublishAot>true</PublishAot> is intended; if AOT is not required, disable it to allow runtime model building.","Re-run the compiled-model generation whenever the model changes so it stays in sync with the DbContext."],"exampleFix":"// before\nvar options = new DbContextOptionsBuilder<AppDb>()\n    .UseSqlServer(connectionString)\n    .Options;\n\n// after (enable compiled model under AOT)\nvar options = new DbContextOptionsBuilder<AppDb>()\n    .UseSqlServer(connectionString)\n    .UseModel(AppDbCompiledModel.Model)\n    .Options;","handlingStrategy":"validation","validationCode":"// Detect AOT early and fail fast with guidance\nif (!RuntimeFeature.IsDynamicCodeSupported\n    && !(optionsBuilder.Options.FindExtension<CoreOptionsExtension>()?.PrecompiledModel ?? false))\n{\n    throw new InvalidOperationException(\"NativeAOT detected: call UseModel(...) with a compiled model.\");\n}","typeGuard":"static bool RequiresCompiledModel()\n    => !System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported;","tryCatchPattern":null,"preventionTips":["Generate the compiled model ('dotnet ef dbcontext optimize') before publishing with <PublishAot>true</PublishAot>.","Regenerate the compiled model whenever the DbContext model changes.","Add a CI step that publishes an AOT build to surface this before release."],"tags":["nativeaot","compiled-model","runtime","relational"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}