Skip to content

Commit

Permalink
Add commands to create compiled models (#24906)
Browse files Browse the repository at this point in the history
Warn when using and old model
Enable NRT-enabled code generation
Remove the need to call FinalizeModel before UseModel
Split out the boilerplate part of the compiled model

Fixes #1906
  • Loading branch information
AndriySvyryd authored May 18, 2021
1 parent a60795c commit 5540433
Show file tree
Hide file tree
Showing 59 changed files with 1,231 additions and 326 deletions.
4 changes: 4 additions & 0 deletions src/EFCore.Design/Design/DbContextActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public static DbContext CreateInstance(
new OperationReporter(reportHandler),
contextType.Assembly,
startupAssembly ?? contextType.Assembly,
projectDir: "",
rootNamespace: null,
language: "C#",
nullable: false,
args: args ?? Array.Empty<string>())
.CreateContext(contextType.FullName!);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static IServiceCollection AddEntityFrameworkDesignTimeServices(
.TryAddSingleton<IModelCodeGeneratorSelector, ModelCodeGeneratorSelector>()
.TryAddSingleton<ICompiledModelCodeGenerator, CSharpRuntimeModelCodeGenerator>()
.TryAddSingleton<ICompiledModelCodeGeneratorSelector, CompiledModelCodeGeneratorSelector>()
.TryAddSingleton<ICompiledModelScaffolder, CompiledModelScaffolder>()
.TryAddSingleton<INamedConnectionStringResolver>(
new DesignTimeConnectionStringResolver(applicationServiceProviderAccessor))
.TryAddSingleton<IPluralizer, HumanizerPluralizer>()
Expand Down
109 changes: 108 additions & 1 deletion src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -27,8 +30,13 @@ public class DbContextOperations
private readonly IOperationReporter _reporter;
private readonly Assembly _assembly;
private readonly Assembly _startupAssembly;
private readonly string _projectDir;
private readonly string? _rootNamespace;
private readonly string? _language;
private readonly bool _nullable;
private readonly string[] _args;
private readonly AppServiceProviderFactory _appServicesFactory;
private readonly DesignTimeServicesBuilder _servicesBuilder;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -40,8 +48,20 @@ public DbContextOperations(
IOperationReporter reporter,
Assembly assembly,
Assembly startupAssembly,
string projectDir,
string? rootNamespace,
string? language,
bool nullable,
string[]? args)
: this(reporter, assembly, startupAssembly, args, new AppServiceProviderFactory(startupAssembly, reporter))
: this(reporter,
assembly,
startupAssembly,
projectDir,
rootNamespace,
language,
nullable,
args,
new AppServiceProviderFactory(startupAssembly, reporter))
{
}

Expand All @@ -55,18 +75,28 @@ protected DbContextOperations(
IOperationReporter reporter,
Assembly assembly,
Assembly startupAssembly,
string projectDir,
string? rootNamespace,
string? language,
bool nullable,
string[]? args,
AppServiceProviderFactory appServicesFactory)
{
Check.NotNull(reporter, nameof(reporter));
Check.NotNull(assembly, nameof(assembly));
Check.NotNull(startupAssembly, nameof(startupAssembly));
Check.NotNull(projectDir, nameof(projectDir));

_reporter = reporter;
_assembly = assembly;
_startupAssembly = startupAssembly;
_projectDir = projectDir;
_rootNamespace = rootNamespace;
_language = language;
_nullable = nullable;
_args = args ?? Array.Empty<string>();
_appServicesFactory = appServicesFactory;
_servicesBuilder = new DesignTimeServicesBuilder(assembly, startupAssembly, reporter, _args);
}

/// <summary>
Expand Down Expand Up @@ -102,6 +132,83 @@ public virtual string ScriptDbContext(string? contextType)
return context.Database.GenerateCreateScript();
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// 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? contextType)
{
using var context = CreateContext(contextType);

var services = _servicesBuilder.Build(context);
var scaffolder = services.GetRequiredService<ICompiledModelScaffolder>();

outputDir = outputDir != null
? Path.GetFullPath(Path.Combine(_projectDir, outputDir))
: _projectDir;

var finalModelNamespace = modelNamespace ?? GetNamespaceFromOutputPath(outputDir) ?? "";

var scaffoldedModel = scaffolder.ScaffoldModel(
context.GetService<IDesignTimeModel>().Model,
outputDir,
new CompiledModelCodeGenerationOptions
{
ContextType = context.GetType(),
ModelNamespace = finalModelNamespace,
Language = _language,
UseNullableReferenceTypes = _nullable
});

var fullName = context.GetType().ShortDisplayName() + "Model";
if (!string.IsNullOrEmpty(modelNamespace))
{
fullName = modelNamespace + "." + fullName;
}

_reporter.WriteInformation(DesignStrings.CompiledModelGenerated($"options.UseModel({fullName}.Instance)"));

var cacheKeyFactory = context.GetService<IModelCacheKeyFactory>();
if (!(cacheKeyFactory is ModelCacheKeyFactory))
{
_reporter.WriteWarning(DesignStrings.CompiledModelCustomCacheKeyFactory(cacheKeyFactory.GetType().ShortDisplayName()));
}

return scaffoldedModel;
}

private string? GetNamespaceFromOutputPath(string directoryPath)
{
var subNamespace = SubnamespaceFromOutputPath(_projectDir, directoryPath);
return string.IsNullOrEmpty(subNamespace)
? _rootNamespace
: string.IsNullOrEmpty(_rootNamespace)
? subNamespace
: _rootNamespace + "." + subNamespace;
}

// if outputDir is a subfolder of projectDir, then use each subfolder as a subnamespace
// --output-dir $(projectFolder)/A/B/C
// => "namespace $(rootnamespace).A.B.C"
private static string? SubnamespaceFromOutputPath(string projectDir, string outputDir)
{
if (!outputDir.StartsWith(projectDir, StringComparison.Ordinal))
{
return null;
}

var subPath = outputDir.Substring(projectDir.Length);

return !string.IsNullOrWhiteSpace(subPath)
? string.Join(
".",
subPath.Split(
new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries))
: null;
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
4 changes: 4 additions & 0 deletions src/EFCore.Design/Design/Internal/MigrationsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public MigrationsOperations(
reporter,
assembly,
startupAssembly,
projectDir,
rootNamespace,
language,
nullable,
_args);

_servicesBuilder = new DesignTimeServicesBuilder(assembly, startupAssembly, reporter, _args);
Expand Down
39 changes: 39 additions & 0 deletions src/EFCore.Design/Design/OperationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ private DbContextOperations ContextOperations
_reporter,
Assembly,
StartupAssembly,
_projectDir,
_rootNamespace,
_language,
_nullable,
_designArgs);

private DatabaseOperations DatabaseOperations
Expand Down Expand Up @@ -480,6 +484,41 @@ private IEnumerable<IDictionary> GetMigrationsImpl(
});
}

/// <summary>
/// Represents an operation to generate a compiled model from the DbContext.
/// </summary>
public class Optimize : OperationBase
{
/// <summary>
/// <para>Initializes a new instance of the <see cref="Optimize" /> class.</para>
/// <para>The arguments supported by <paramref name="args" /> are:</para>
/// <para><c>outputDir</c>--The directory to put files in. Paths are relative to the project directory.</para>
/// <para><c>modelNamespace</c>--Specify to override the namespace of the generated model.</para>
/// <para><c>contextType</c>--The <see cref="DbContext" /> to use.</para>
/// </summary>
/// <param name="executor"> The operation executor. </param>
/// <param name="resultHandler"> The <see cref="IOperationResultHandler" />. </param>
/// <param name="args"> The operation arguments. </param>
public Optimize(
OperationExecutor executor,
IOperationResultHandler resultHandler,
IDictionary args)
: base(resultHandler)
{
Check.NotNull(executor, nameof(executor));
Check.NotNull(args, nameof(args));

var outputDir = (string?)args["outputDir"];
var modelNamespace = (string?)args["modelNamespace"];
var contextType = (string?)args["contextType"];

Execute(() => executor.OptimizeImpl(outputDir, modelNamespace, contextType));
}
}

private IReadOnlyList<string> OptimizeImpl(string? outputDir, string? modelNamespace, string? contextType)
=> ContextOperations.Optimize(outputDir, modelNamespace, contextType);

/// <summary>
/// Represents an operation to scaffold a <see cref="DbContext" /> and entity types for a database.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ public SnapshotModelProcessor(
}
}

if (model is IMutableModel mutableModel)
{
model = mutableModel.FinalizeModel();
}

return _modelRuntimeInitializer.Initialize((IModel)model, designTime: true, validationLogger: null);
}

Expand Down
16 changes: 16 additions & 0 deletions src/EFCore.Design/Properties/DesignStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/EFCore.Design/Properties/DesignStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,15 @@
<data name="CompiledModelConstructorBinding" xml:space="preserve">
<value>The entity type '{entityType}' has a custom constructor binding. This is usually caused by using proxies. Compiled model can't be generated, because dynamic proxy types are not supported. If you are not using proxies configure the custom constructor binding in '{customize}' in a partial '{className}' class instead.</value>
</data>
<data name="CompiledModelCustomCacheKeyFactory" xml:space="preserve">
<value>The context is configured to use a custom model cache key factory '{factoryType}', this usually indicates that the produced model can change between context instances. To preserve this behavior manually modify the generated compiled model source code.</value>
</data>
<data name="CompiledModelDefiningQuery" xml:space="preserve">
<value>The entity type '{entityType}' has a defining query configured. Compiled model can't be generated, because defining queries are not supported.</value>
</data>
<data name="CompiledModelGenerated" xml:space="preserve">
<value>Successfully generated a compiled model, to use it call '{optionsCall}'. Run this command again when the model is modified.</value>
</data>
<data name="CompiledModelQueryFilter" xml:space="preserve">
<value>The entity type '{entityType}' has a query filter configured. Compiled model can't be generated, because query filters are not supported.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,11 @@ public class CompiledModelCodeGenerationOptions
/// </summary>
/// <value> The programming language to scaffold for. </value>
public virtual string? Language { get; set; }

/// <summary>
/// Gets or sets a value indicating whether nullable reference types are enabled.
/// </summary>
/// <value> A value indicating whether nullable reference types are enabled. </value>
public virtual bool UseNullableReferenceTypes { get; set; }
}
}
26 changes: 26 additions & 0 deletions src/EFCore.Design/Scaffolding/ICompiledModelScaffolder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Metadata;

namespace Microsoft.EntityFrameworkCore.Scaffolding
{
/// <summary>
/// Used to scaffold a compiled model from a model.
/// </summary>
public interface ICompiledModelScaffolder
{
/// <summary>
/// Scaffolds a compiled model from a model and saves it to disk.
/// </summary>
/// <param name="model"> The model. </param>
/// <param name="outputDir"> The output directory. </param>
/// <param name="options"> The options to use when generating code for the model. </param>
/// <returns> The scaffolded model files. </returns>
IReadOnlyList<string> ScaffoldModel(
IModel model,
string outputDir,
CompiledModelCodeGenerationOptions options);
}
}
Loading

0 comments on commit 5540433

Please sign in to comment.