Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow suppression of OnConfiguring() generation. #20811

Merged
merged 6 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/EFCore.Design/Design/Internal/DatabaseOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public virtual SavedModelFiles ScaffoldContext(
[CanBeNull] string contextNamespace,
bool useDataAnnotations,
bool overwriteFiles,
bool useDatabaseNames)
bool useDatabaseNames,
bool suppressOnConfiguring)
{
Check.NotEmpty(provider, nameof(provider));
Check.NotEmpty(connectionString, nameof(connectionString));
Expand Down Expand Up @@ -113,7 +114,8 @@ public virtual SavedModelFiles ScaffoldContext(
ContextNamespace = finalContextNamespace,
Language = _language,
ContextDir = MakeDirRelative(outputDir, outputContextDir),
ContextName = dbContextClassName
ContextName = dbContextClassName,
SuppressOnConfiguring = suppressOnConfiguring
});

return scaffolder.Save(
Expand Down
8 changes: 5 additions & 3 deletions src/EFCore.Design/Design/OperationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,13 @@ public ScaffoldContext(
var useDataAnnotations = (bool)args["useDataAnnotations"];
var overwriteFiles = (bool)args["overwriteFiles"];
var useDatabaseNames = (bool)args["useDatabaseNames"];
var suppressOnConfiguring = (bool)(args["suppressOnConfiguring"] ?? false);

Execute(
() => executor.ScaffoldContextImpl(
provider, connectionString, outputDir, outputDbContextDir, dbContextClassName,
schemaFilters, tableFilters, modelNamespace, contextNamespace, useDataAnnotations,
overwriteFiles, useDatabaseNames));
overwriteFiles, useDatabaseNames, suppressOnConfiguring));
}
}

Expand All @@ -492,7 +493,8 @@ private IDictionary ScaffoldContextImpl(
[CanBeNull] string contextNamespace,
bool useDataAnnotations,
bool overwriteFiles,
bool useDatabaseNames)
bool useDatabaseNames,
bool suppressOnConfiguring)
{
Check.NotNull(provider, nameof(provider));
Check.NotNull(connectionString, nameof(connectionString));
Expand All @@ -502,7 +504,7 @@ private IDictionary ScaffoldContextImpl(
var files = DatabaseOperations.ScaffoldContext(
provider, connectionString, outputDir, outputDbContextDir, dbContextClassName,
schemaFilters, tableFilters, modelNamespace, contextNamespace, useDataAnnotations,
overwriteFiles, useDatabaseNames);
overwriteFiles, useDatabaseNames, suppressOnConfiguring);

return new Hashtable { ["ContextFile"] = files.ContextFile, ["EntityTypeFiles"] = files.AdditionalFiles.ToArray() };
}
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Design/Properties/DesignStrings.Designer.cs

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

2 changes: 1 addition & 1 deletion src/EFCore.Design/Properties/DesignStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.U
Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.</value>
</data>
<data name="SensitiveInformationWarning" xml:space="preserve">
<value>To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.</value>
<value>To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.</value>
<comment>Localize the URL if we have localized docs.</comment>
</data>
<data name="ForceRemoveMigration" xml:space="preserve">
Expand Down
14 changes: 10 additions & 4 deletions src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public virtual string WriteCode(
string? contextNamespace,
string modelNamespace,
bool useDataAnnotations,
bool suppressConnectionStringWarning)
bool suppressConnectionStringWarning,
bool suppressOnConfiguring)
{
Check.NotNull(model, nameof(model));

Expand Down Expand Up @@ -97,7 +98,8 @@ public virtual string WriteCode(
contextName,
connectionString,
useDataAnnotations,
suppressConnectionStringWarning);
suppressConnectionStringWarning,
suppressOnConfiguring);
}

_sb.AppendLine("}");
Expand All @@ -116,7 +118,8 @@ protected virtual void GenerateClass(
[NotNull] string contextName,
[NotNull] string connectionString,
bool useDataAnnotations,
bool suppressConnectionStringWarning)
bool suppressConnectionStringWarning,
bool suppressOnConfiguring)
{
Check.NotNull(model, nameof(model));
Check.NotNull(contextName, nameof(contextName));
Expand All @@ -130,7 +133,10 @@ protected virtual void GenerateClass(
GenerateConstructors(contextName);
GenerateDbSets(model);
GenerateEntityTypeErrors(model);
GenerateOnConfiguring(connectionString, suppressConnectionStringWarning);
if (!suppressOnConfiguring)
{
GenerateOnConfiguring(connectionString, suppressConnectionStringWarning);
}
GenerateOnModelCreating(model, useDataAnnotations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public override ScaffoldedModel GenerateModel(
options.ContextNamespace,
options.ModelNamespace,
options.UseDataAnnotations,
options.SuppressConnectionStringWarning);
options.SuppressConnectionStringWarning,
options.SuppressOnConfiguring);

// output DbContext .cs file
var dbContextFileName = options.ContextName + FileExtension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ string WriteCode(
[CanBeNull] string? contextNamespace,
[NotNull] string modelNamespace,
bool useDataAnnotations,
bool suppressConnectionStringWarning);
bool suppressConnectionStringWarning,
bool suppressOnConfiguring);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
Expand All @@ -32,6 +33,7 @@ public class ReverseEngineerScaffolder : IReverseEngineerScaffolder
private readonly ICSharpUtilities _cSharpUtilities;
private readonly ICSharpHelper _code;
private readonly INamedConnectionStringResolver _connectionStringResolver;
private readonly IOperationReporter _reporter;
private const string DbContextSuffix = "Context";
private const string DefaultDbContextName = "Model" + DbContextSuffix;

Expand All @@ -47,21 +49,24 @@ public ReverseEngineerScaffolder(
[NotNull] IModelCodeGeneratorSelector modelCodeGeneratorSelector,
[NotNull] ICSharpUtilities cSharpUtilities,
[NotNull] ICSharpHelper cSharpHelper,
[NotNull] INamedConnectionStringResolver connectionStringResolver)
[NotNull] INamedConnectionStringResolver connectionStringResolver,
[NotNull] IOperationReporter reporter)
{
Check.NotNull(databaseModelFactory, nameof(databaseModelFactory));
Check.NotNull(scaffoldingModelFactory, nameof(scaffoldingModelFactory));
Check.NotNull(modelCodeGeneratorSelector, nameof(modelCodeGeneratorSelector));
Check.NotNull(cSharpUtilities, nameof(cSharpUtilities));
Check.NotNull(cSharpHelper, nameof(cSharpHelper));
Check.NotNull(connectionStringResolver, nameof(connectionStringResolver));
Check.NotNull(reporter, nameof(reporter));

_databaseModelFactory = databaseModelFactory;
_factory = scaffoldingModelFactory;
ModelCodeGeneratorSelector = modelCodeGeneratorSelector;
_cSharpUtilities = cSharpUtilities;
_code = cSharpHelper;
_connectionStringResolver = connectionStringResolver;
_reporter = reporter;
}

/// <summary>
Expand Down Expand Up @@ -102,6 +107,10 @@ public virtual ScaffoldedModel ScaffoldModel(
{
codeOptions.SuppressConnectionStringWarning = true;
}
else if (!codeOptions.SuppressOnConfiguring)
{
_reporter.WriteWarning(DesignStrings.SensitiveInformationWarning);
}

if (codeOptions.ConnectionString == null)
{
Expand Down
6 changes: 6 additions & 0 deletions src/EFCore.Design/Scaffolding/ModelCodeGenerationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public class ModelCodeGenerationOptions
/// <value> A value indicating whether to suppress the connection string sensitive information warning. </value>
public virtual bool SuppressConnectionStringWarning { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to suppress generation of the OnConfiguring() method.
/// </summary>
/// <value> A value indicating whether to suppress generation of the OnConfiguring() method. </value>
public virtual bool SuppressOnConfiguring { get; set; }

/// <summary>
/// Gets or sets the namespace of the project.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions src/EFCore.Tools/tools/EntityFrameworkCore.PS2.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ function Remove-Migration(
.PARAMETER Force
Overwrite existing files.
.PARAMETER NoOnConfiguring
Suppress generation of the DbContext.OnConfiguring() method.
.PARAMETER Project
The project to use.
Expand All @@ -201,6 +204,7 @@ function Scaffold-DbContext(
[switch] $DataAnnotations,
[switch] $UseDatabaseNames,
[switch] $Force,
[switch] $NoOnConfiguring,
$Project,
$StartupProject,
$Namespace,
Expand Down
9 changes: 9 additions & 0 deletions src/EFCore.Tools/tools/EntityFrameworkCore.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ Register-TabExpansion Scaffold-DbContext @{
.PARAMETER Force
Overwrite existing files.
.PARAMETER NoOnConfiguring
Suppress generation of the DbContext.OnConfiguring() method.
.PARAMETER Project
The project to use.
Expand Down Expand Up @@ -393,6 +396,7 @@ function Scaffold-DbContext
[switch] $DataAnnotations,
[switch] $UseDatabaseNames,
[switch] $Force,
[switch] $NoOnConfiguring,
[string] $Project,
[string] $StartupProject,
[string] $Namespace,
Expand Down Expand Up @@ -448,6 +452,11 @@ function Scaffold-DbContext
$params += '--force'
}

if ($NoOnConfiguring)
{
$params += '--no-onconfiguring'
}

if ($RemainingArguments -ne $null)
{
$params += $RemainingArguments
Expand Down
6 changes: 6 additions & 0 deletions src/dotnet-ef/Properties/Resources.Designer.cs

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

3 changes: 3 additions & 0 deletions src/dotnet-ef/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,7 @@
<data name="MigrationsNamespaceDescription" xml:space="preserve">
<value>Specify to override the namespace for the migration.</value>
</data>
<data name="SuppressOnConfiguringDescription" xml:space="preserve">
<value>Suppress generation of the DbContext.OnConfiguring() method.</value>
</data>
</root>
2 changes: 2 additions & 0 deletions src/ef/Commands/DbContextScaffoldCommand.Configure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal partial class DbContextScaffoldCommand : ProjectCommandBase
private CommandOption _json;
private CommandOption _namespace;
private CommandOption _contextNamespace;
private CommandOption _suppressOnConfiguring;

public override void Configure(CommandLineApplication command)
{
Expand All @@ -40,6 +41,7 @@ public override void Configure(CommandLineApplication command)
_json = Json.ConfigureOption(command);
_namespace = command.Option("-n|--namespace <NAMESPACE>", Resources.NamespaceDescription);
_contextNamespace = command.Option("--context-namespace <NAMESPACE>", Resources.ContextNamespaceDescription);
_suppressOnConfiguring = command.Option("--no-onconfiguring", Resources.SuppressOnConfiguringDescription);

base.Configure(command);
}
Expand Down
3 changes: 2 additions & 1 deletion src/ef/Commands/DbContextScaffoldCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ protected override int Execute(string[] args)
_force.HasValue(),
_useDatabaseNames.HasValue(),
_namespace.Value(),
_contextNamespace.Value());
_contextNamespace.Value(),
_suppressOnConfiguring.HasValue());
if (_json.HasValue())
{
ReportJsonResults(result);
Expand Down
3 changes: 2 additions & 1 deletion src/ef/IOperationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ IDictionary ScaffoldContext(
bool overwriteFiles,
bool useDatabaseNames,
string entityNamespace,
string dbContextNamespace);
string dbContextNamespace,
bool suppressOnConfiguring);

string ScriptMigration(string fromMigration, string toMigration, bool idempotent, string contextType);

Expand Down
6 changes: 4 additions & 2 deletions src/ef/OperationExecutorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public IDictionary ScaffoldContext(
bool overwriteFiles,
bool useDatabaseNames,
string modelNamespace,
string contextNamespace)
string contextNamespace,
bool suppressOnConfiguring)
=> InvokeOperation<IDictionary>(
"ScaffoldContext",
new Dictionary<string, object>
Expand All @@ -159,7 +160,8 @@ public IDictionary ScaffoldContext(
["overwriteFiles"] = overwriteFiles,
["useDatabaseNames"] = useDatabaseNames,
["modelNamespace"] = modelNamespace,
["contextNamespace"] = contextNamespace
["contextNamespace"] = contextNamespace,
["suppressOnConfiguring"] = suppressOnConfiguring
});

public string ScriptMigration(
Expand Down
6 changes: 6 additions & 0 deletions src/ef/Properties/Resources.Designer.cs

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

3 changes: 3 additions & 0 deletions src/ef/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,7 @@
<data name="RemainingArguments" xml:space="preserve">
<value>Remaining arguments: '{remainingArguments}'.</value>
</data>
<data name="SuppressOnConfiguringDescription" xml:space="preserve">
<value>Suppress generation of the DbContext.OnConfiguring() method.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,48 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
model => Assert.Empty(model.GetEntityTypes()));
}

[ConditionalFact]
public void SuppressOnConfiguring_works()
{
Test(
modelBuilder => { },
new ModelCodeGenerationOptions { SuppressOnConfiguring = true },
code =>
{
Assert.Equal(
@"using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace TestNamespace
{
public partial class TestDbContext : DbContext
{
public TestDbContext()
{
}

public TestDbContext(DbContextOptions<TestDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
OnModelCreatingPartial(modelBuilder);
}

partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
}
",
code.ContextFile.Code,
ignoreLineEndingDifferences: true);

Assert.Empty(code.AdditionalFiles);
});
}

[ConditionalFact]
public void Plugins_work()
{
Expand Down
Loading