diff --git a/src/EFCore.Tasks/README.md b/src/EFCore.Tasks/README.md
index e23d373b089..b32333a35da 100644
--- a/src/EFCore.Tasks/README.md
+++ b/src/EFCore.Tasks/README.md
@@ -4,7 +4,7 @@ This package should be referenced by the project containing the derived `DbConte
## Usage
-Install the package into your project, set `true` and then run build normally.
+Install the package into your project and if PublishAOT is true then just publish normally. Otherwise you can control code generation by the `$(EFScaffoldModelStage)` and `$(EFPrecompileQueriesStage)` properties, which can be set to either `publish` or `build` to control at what stage the code will be generated. Any other value will disable the corresponding generation.
If the startup project is different from the current project it needs to be specified: `..\Startup\Startup.csproj`
diff --git a/src/EFCore.Tasks/buildTransitive/Microsoft.EntityFrameworkCore.Tasks.targets b/src/EFCore.Tasks/buildTransitive/Microsoft.EntityFrameworkCore.Tasks.targets
index 1032f4edbea..cbf43363318 100644
--- a/src/EFCore.Tasks/buildTransitive/Microsoft.EntityFrameworkCore.Tasks.targets
+++ b/src/EFCore.Tasks/buildTransitive/Microsoft.EntityFrameworkCore.Tasks.targets
@@ -20,7 +20,6 @@
+ Condition="'$(_EFGenerationStage)'=='' And ('$(EFScaffoldModelStage)'=='build' Or '$(EFPrecompileQueriesStage)'=='build')">
+ Condition="'$(_EFGenerationStage)'=='' And ('$(EFScaffoldModelStage)'=='publish' Or '$(EFPrecompileQueriesStage)'=='publish' Or '$(_EFPublishAOT)'=='true' Or '$(PublishAOT)'=='true')">
+
@@ -223,4 +224,4 @@ For Publish:
-
\ No newline at end of file
+
diff --git a/src/dotnet-ef/RootCommand.cs b/src/dotnet-ef/RootCommand.cs
index d019cf8c068..1e644f83875 100644
--- a/src/dotnet-ef/RootCommand.cs
+++ b/src/dotnet-ef/RootCommand.cs
@@ -84,7 +84,7 @@ protected override int Execute(string[] _)
&& _args[0] == "dbcontext"
&& _args[1] == "optimize"
&& !_args.Any(a => a == "--no-scaffold");
- startupProject.Build(skipOptimization ? new[] { "/p:EFOptimizeContext=false" } : null);
+ startupProject.Build(skipOptimization ? ["/p:EFScaffoldModelStage=none", "/p:EFPrecompileQueriesStage=none"] : null);
Reporter.WriteInformation(Resources.BuildSucceeded);
}