Skip to content

Commit

Permalink
Don't generate code for precached values in the compiled model by def…
Browse files Browse the repository at this point in the history
…ault (#34412)

Add --nativeaot CLI option to output the eager code
  • Loading branch information
AndriySvyryd authored Aug 14, 2024
1 parent 37599d2 commit e468fb7
Show file tree
Hide file tree
Showing 125 changed files with 3,003 additions and 2,211 deletions.
15 changes: 13 additions & 2 deletions src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ public virtual string ScriptDbContext(string? contextType)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual IReadOnlyList<string> Optimize(
string? outputDir, string? modelNamespace, string? contextTypeName, string? suffix, bool scaffoldModel, bool precompileQueries)
string? outputDir,
string? modelNamespace,
string? contextTypeName,
string? suffix,
bool scaffoldModel,
bool precompileQueries,
bool nativeAot)
{
var optimizeAllInAssembly = contextTypeName == "*";
var contexts = optimizeAllInAssembly ? CreateAllContexts() : [CreateContext(contextTypeName)];
Expand All @@ -152,6 +158,7 @@ public virtual IReadOnlyList<string> Optimize(
precompileQueries,
context,
optimizeAllInAssembly,
nativeAot,
generatedFiles,
generatedFileNames);
contextOptimized = true;
Expand Down Expand Up @@ -182,6 +189,7 @@ private void Optimize(
bool precompileQueries,
DbContext context,
bool optimizeAllInAssembly,
bool nativeAot,
List<string> generatedFiles,
HashSet<string> generatedFileNames)
{
Expand All @@ -193,7 +201,8 @@ private void Optimize(
if (scaffoldModel
&& (!optimizeAllInAssembly || contextType.Assembly == _assembly))
{
generatedFiles.AddRange(ScaffoldCompiledModel(outputDir, modelNamespace, context, suffix, services, generatedFileNames));
generatedFiles.AddRange(ScaffoldCompiledModel(
outputDir, modelNamespace, context, suffix, nativeAot, services, generatedFileNames));
if (precompileQueries)
{
memberAccessReplacements = ((IRuntimeModel)context.GetService<IDesignTimeModel>().Model).GetUnsafeAccessors();
Expand All @@ -217,6 +226,7 @@ private IReadOnlyList<string> ScaffoldCompiledModel(
string? modelNamespace,
DbContext context,
string? suffix,
bool nativeAot,
IServiceProvider services,
ISet<string> generatedFileNames)
{
Expand Down Expand Up @@ -255,6 +265,7 @@ private IReadOnlyList<string> ScaffoldCompiledModel(
Language = _language,
UseNullableReferenceTypes = _nullable,
Suffix = suffix,
ForNativeAot = nativeAot,
GeneratedFileNames = generatedFileNames
});

Expand Down
22 changes: 18 additions & 4 deletions src/EFCore.Design/Design/OperationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,13 +541,27 @@ public OptimizeContext(
var suffix = (string?)args["suffix"];
var scaffoldModel = (bool)(args["scaffoldModel"] ?? true);
var precompileQueries = (bool)(args["precompileQueries"] ?? false);

Execute(() => executor.OptimizeContextImpl(outputDir, modelNamespace, contextType, suffix, scaffoldModel, precompileQueries));
var nativeAot = (bool)(args["nativeAot"] ?? false);

Execute(() => executor.OptimizeContextImpl(
outputDir,
modelNamespace,
contextType,
suffix,
scaffoldModel,
precompileQueries,
nativeAot));
}
}
private IReadOnlyList<string> OptimizeContextImpl(
string? outputDir, string? modelNamespace, string? contextType, string? suffix, bool scaffoldModel, bool precompileQueries)
=> ContextOperations.Optimize(outputDir, modelNamespace, contextType, suffix, scaffoldModel, precompileQueries);
string? outputDir,
string? modelNamespace,
string? contextType,
string? suffix,
bool scaffoldModel,
bool precompileQueries,
bool nativeAot)
=> ContextOperations.Optimize(outputDir, modelNamespace, contextType, suffix, scaffoldModel, precompileQueries, nativeAot);

/// <summary>
/// Represents an operation to scaffold a <see cref="DbContext" /> and entity types for a database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public class CompiledModelCodeGenerationOptions
/// <value> The suffix to attach to the name of all the generated files. </value>
public virtual string? Suffix { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the generated code should be compatible with NativeAOT.
/// </summary>
/// <value> A value indicating whether the generated code should be compatible with NativeAOT. </value>
public virtual bool ForNativeAot { get; set; }

/// <summary>
/// Gets or sets the set of file names generated so far.
/// </summary>
Expand Down
Loading

0 comments on commit e468fb7

Please sign in to comment.