entity-framework/core/cli/msbuild.md
Starting with EF 9, you can use an MSBuild task to generate the compiled model and precompiled queries automatically either when the project is built or when it's published. This is mainly intended to be used with NativeAOT publishing.
[!WARNING] NativeAOT support and the MSBuild integration are experimental features, and are not yet suited for production use.
To get started, install the Microsoft.EntityFrameworkCore.Tasks NuGet package. For example:
dotnet add package Microsoft.EntityFrameworkCore.Tasks
[!NOTE] Every project that needs to be compiled with generated files should reference the NuGet package, it is not transitive by default.
If the project specifies <PublishAot>true</PublishAot> then by default the MSBuild task will generate a compiled model and precompiled queries during publishing. Otherwise, you can set the following properties to control the generation behavior:
| MSBuild property | Description |
|---|---|
| EFOptimizeContext | Set to true to enable MSBuild integration. |
| EFScaffoldModelStage | Set to publish, build or none to indicate at which stage the compiled model will be generated. Defaults to publish. |
| EFPrecompileQueriesStage | Set to publish, build or none to indicate at which stage the precompiled queries will be generated. Defaults to publish. |
| DbContextName | The derived DbContext class to use. Class name only or fully qualified with namespaces. If this option is omitted, EF Core will perform generation for all context classes in the project. |
| EFTargetNamespace | The namespace to use for all generated classes. If this option is omitted, EF Core will use $(RootNamespace). |
| EFOutputDir | The folder to put the generated files before the project is compiled. If this option is omitted, EF Core will use $(IntermediateOutputPath). |
| EFNullable | Whether nullable reference types will be used in the generated code. If this option is omitted, EF Core will use $(Nullable). |
publish stage also set the rid in the startup project (e.g. <RuntimeIdentifier>win-x64</RuntimeIdentifier>)