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

Fix for 8332. Pass in arguments to IDesignTimeDbContextFactory #20341

Merged
merged 1 commit into from
Apr 25, 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/DbContextActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ public static class DbContextActivator
/// <param name="contextType"> The <see cref="DbContext" /> type to instantiate. </param>
/// <param name="startupAssembly"> The application's startup assembly. </param>
/// <param name="reportHandler"> The design-time report handler. </param>
/// <param name="arguments"> Arguments passed to the application. </param>
/// <returns> The newly created object. </returns>
public static DbContext CreateInstance(
[NotNull] Type contextType,
[CanBeNull] Assembly startupAssembly = null,
[CanBeNull] IOperationReportHandler reportHandler = null)
[CanBeNull] IOperationReportHandler reportHandler = null,
[CanBeNull] string[] arguments = null)
{
Check.NotNull(contextType, nameof(contextType));

return new DbContextOperations(
new OperationReporter(reportHandler),
contextType.Assembly,
startupAssembly ?? contextType.Assembly,
args: Array.Empty<string>()) // TODO: Issue #8332
args: arguments ?? Array.Empty<string>())
.CreateContext(contextType.FullName);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/EFCore.Design/Design/Internal/DatabaseOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class DatabaseOperations
private readonly string _rootNamespace;
private readonly string _language;
private readonly DesignTimeServicesBuilder _servicesBuilder;
private readonly string[] _args;
lajones marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -51,8 +52,9 @@ public DatabaseOperations(
_projectDir = projectDir;
_rootNamespace = rootNamespace;
_language = language;
_args = args;

_servicesBuilder = new DesignTimeServicesBuilder(assembly, startupAssembly, reporter, args);
_servicesBuilder = new DesignTimeServicesBuilder(assembly, startupAssembly, reporter, _args);
}

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions src/EFCore.Design/Design/Internal/MigrationsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class MigrationsOperations
private readonly string _language;
private readonly DesignTimeServicesBuilder _servicesBuilder;
private readonly DbContextOperations _contextOperations;
private readonly string[] _args;
lajones marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -60,13 +61,14 @@ public MigrationsOperations(
_projectDir = projectDir;
_rootNamespace = rootNamespace;
_language = language;
_args = args;
_contextOperations = new DbContextOperations(
reporter,
assembly,
startupAssembly,
args);
_args);

_servicesBuilder = new DesignTimeServicesBuilder(assembly, startupAssembly, reporter, args);
_servicesBuilder = new DesignTimeServicesBuilder(assembly, startupAssembly, reporter, _args);
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/EFCore.Design/Design/OperationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class OperationExecutor : MarshalByRefObject
/// <para><c>startupTargetName</c>--The assembly name of the startup project.</para>
/// <para><c>projectDir</c>--The target project's root directory.</para>
/// <para><c>rootNamespace</c>--The target project's root namespace.</para>
/// <para><c>language</c>--The programming language to be used to generate classes.</para>
/// <para><c>remainingArguments</c>--Extra arguments passed into the operation.</para>
/// </summary>
/// <param name="reportHandler"> The <see cref="IOperationReportHandler" />. </param>
/// <param name="args"> The executor arguments. </param>
Expand All @@ -61,9 +63,7 @@ public OperationExecutor([NotNull] IOperationReportHandler reportHandler, [NotNu
_projectDir = (string)args["projectDir"];
_rootNamespace = (string)args["rootNamespace"];
_language = (string)args["language"];

// TODO: Flow in from tools (issue #8332)
_designArgs = Array.Empty<string>();
_designArgs = (string[])args["remainingArguments"];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should coalesce to empty array.


var toolsVersion = (string)args["toolsVersion"];
var runtimeVersion = ProductInfo.GetVersion();
Expand Down
75 changes: 67 additions & 8 deletions src/EFCore.Tools/tools/EntityFrameworkCore.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Register-TabExpansion Add-Migration @{
.PARAMETER Namespace
Specify to override the namespace for the migration.

.PARAMETER RemainingArguments
Arguments passed to the application.

.LINK
Remove-Migration
Update-Database
Expand All @@ -51,7 +54,9 @@ function Add-Migration
[string] $Context,
[string] $Project,
[string] $StartupProject,
[string] $Namespace)
[string] $Namespace,
[Parameter(ValueFromRemainingArguments = $true)]
[string[]] $RemainingArguments)
lajones marked this conversation as resolved.
Show resolved Hide resolved

WarnIfEF6 'Add-Migration'

Expand All @@ -71,6 +76,7 @@ function Add-Migration
}

$params += GetParams $Context
$params += $RemainingArguments
lajones marked this conversation as resolved.
Show resolved Hide resolved

# NB: -join is here to support ConvertFrom-Json on PowerShell 3.0
$result = (EF $dteProject $dteStartupProject $params) -join "`n" | ConvertFrom-Json
Expand Down Expand Up @@ -111,14 +117,22 @@ Register-TabExpansion Drop-Database @{
.PARAMETER StartupProject
The startup project to use. Defaults to the solution's startup project.

.PARAMETER RemainingArguments
Arguments passed to the application.

.LINK
Update-Database
about_EntityFrameworkCore
#>
function Drop-Database
{
[CmdletBinding(PositionalBinding = $false, SupportsShouldProcess = $true, ConfirmImpact = 'High')]
param([string] $Context, [string] $Project, [string] $StartupProject)
param(
[string] $Context,
[string] $Project,
[string] $StartupProject,
[Parameter(ValueFromRemainingArguments = $true)]
[string[]] $RemainingArguments)

$dteProject = GetProject $Project
$dteStartupProject = GetStartupProject $StartupProject $dteProject
Expand All @@ -129,6 +143,7 @@ function Drop-Database
{
$params = 'database', 'drop', '--force'
$params += GetParams $Context
$params += $RemainingArguments

EF $dteProject $dteStartupProject $params -skipBuild
}
Expand Down Expand Up @@ -170,13 +185,21 @@ Register-TabExpansion Get-DbContext @{
.PARAMETER StartupProject
The startup project to use. Defaults to the solution's startup project.

.PARAMETER RemainingArguments
Arguments passed to the application.

.LINK
about_EntityFrameworkCore
#>
function Get-DbContext
{
[CmdletBinding(PositionalBinding = $false)]
param([string] $Context, [string] $Project, [string] $StartupProject)
param(
[string] $Context,
[string] $Project,
[string] $StartupProject,
[Parameter(ValueFromRemainingArguments = $true)]
[string[]] $RemainingArguments)

$dteProject = GetProject $Project
$dteStartupProject = GetStartupProject $StartupProject $dteProject
Expand All @@ -185,6 +208,7 @@ function Get-DbContext
{
$params = 'dbcontext', 'info', '--json'
$params += GetParams $Context
$params += $RemainingArguments
# NB: -join is here to support ConvertFrom-Json on PowerShell 3.0
return (EF $dteProject $dteStartupProject $params) -join "`n" | ConvertFrom-Json
}
Expand Down Expand Up @@ -225,14 +249,23 @@ Register-TabExpansion Remove-Migration @{
.PARAMETER StartupProject
The startup project to use. Defaults to the solution's startup project.

.PARAMETER RemainingArguments
Arguments passed to the application.

.LINK
Add-Migration
about_EntityFrameworkCore
#>
function Remove-Migration
{
[CmdletBinding(PositionalBinding = $false)]
param([switch] $Force, [string] $Context, [string] $Project, [string] $StartupProject)
param(
[switch] $Force,
[string] $Context,
[string] $Project,
[string] $StartupProject,
[Parameter(ValueFromRemainingArguments = $true)]
[string[]] $RemainingArguments)

$dteProject = GetProject $Project
$dteStartupProject = GetStartupProject $StartupProject $dteProject
Expand All @@ -245,6 +278,7 @@ function Remove-Migration
}

$params += GetParams $Context
$params += $RemainingArguments

# NB: -join is here to support ConvertFrom-Json on PowerShell 3.0
$result = (EF $dteProject $dteStartupProject $params) -join "`n" | ConvertFrom-Json
Expand Down Expand Up @@ -320,6 +354,9 @@ Register-TabExpansion Scaffold-DbContext @{
.PARAMETER ContextNamespace
Specify to override the namespace for the DbContext class.

.PARAMETER RemainingArguments
Arguments passed to the application.

.LINK
about_EntityFrameworkCore
#>
Expand All @@ -342,7 +379,9 @@ function Scaffold-DbContext
[string] $Project,
[string] $StartupProject,
[string] $Namespace,
[string] $ContextNamespace)
[string] $ContextNamespace,
[Parameter(ValueFromRemainingArguments = $true)]
[string[]] $RemainingArguments)

$dteProject = GetProject $Project
$dteStartupProject = GetStartupProject $StartupProject $dteProject
Expand Down Expand Up @@ -392,6 +431,8 @@ function Scaffold-DbContext
$params += '--force'
}

$params += $RemainingArguments

# NB: -join is here to support ConvertFrom-Json on PowerShell 3.0
$result = (EF $dteProject $dteStartupProject $params) -join "`n" | ConvertFrom-Json

Expand Down Expand Up @@ -430,6 +471,9 @@ Register-TabExpansion Script-DbContext @{
.PARAMETER StartupProject
The startup project to use. Defaults to the solution's startup project.

.PARAMETER RemainingArguments
Arguments passed to the application.

.LINK
about_EntityFrameworkCore
#>
Expand All @@ -440,7 +484,9 @@ function Script-DbContext
[string] $Output,
[string] $Context,
[string] $Project,
[string] $StartupProject)
[string] $StartupProject,
[Parameter(ValueFromRemainingArguments = $true)]
[string[]] $RemainingArguments)

$dteProject = GetProject $Project
$dteStartupProject = GetStartupProject $StartupProject $dteProject
Expand All @@ -465,6 +511,7 @@ function Script-DbContext
$params = 'dbcontext', 'script', '--output', $Output

$params += GetParams $Context
$params += $RemainingArguments

EF $dteProject $dteStartupProject $params

Expand Down Expand Up @@ -512,6 +559,9 @@ Register-TabExpansion Script-Migration @{
.PARAMETER StartupProject
The startup project to use. Defaults to the solution's startup project.

.PARAMETER RemainingArguments
Arguments passed to the application.

.LINK
Update-Database
about_EntityFrameworkCore
Expand All @@ -529,7 +579,9 @@ function Script-Migration
[string] $Output,
[string] $Context,
[string] $Project,
[string] $StartupProject)
[string] $StartupProject,
[Parameter(ValueFromRemainingArguments = $true)]
[string[]] $RemainingArguments)

$dteProject = GetProject $Project
$dteStartupProject = GetStartupProject $StartupProject $dteProject
Expand Down Expand Up @@ -569,6 +621,7 @@ function Script-Migration
}

$params += GetParams $Context
$params += $RemainingArguments

EF $dteProject $dteStartupProject $params

Expand Down Expand Up @@ -609,6 +662,9 @@ Register-TabExpansion Update-Database @{
.PARAMETER StartupProject
The startup project to use. Defaults to the solution's startup project.

.PARAMETER RemainingArguments
Arguments passed to the application.

.LINK
Script-Migration
about_EntityFrameworkCore
Expand All @@ -622,7 +678,9 @@ function Update-Database
[string] $Connection,
[string] $Context,
[string] $Project,
[string] $StartupProject)
[string] $StartupProject,
[Parameter(ValueFromRemainingArguments = $true)]
[string[]] $RemainingArguments)

WarnIfEF6 'Update-Database'

Expand All @@ -642,6 +700,7 @@ function Update-Database
}

$params += GetParams $Context
$params += $RemainingArguments

EF $dteProject $dteStartupProject $params
}
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-ef/RootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override void Configure(CommandLineApplication command)
_command = command;
}

protected override int Execute()
protected override int Execute(string[] _)
{
var commands = _args.TakeWhile(a => a[0] != '-').ToList();
if (_help.HasValue()
Expand Down
8 changes: 5 additions & 3 deletions src/ef/AppDomainOperationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public AppDomainOperationExecutor(
string projectDir,
string dataDirectory,
string rootNamespace,
string language)
: base(assembly, startupAssembly, projectDir, rootNamespace, language)
string language,
string[] remainingArguments)
: base(assembly, startupAssembly, projectDir, rootNamespace, language, remainingArguments)
{
var info = new AppDomainSetup { ApplicationBase = AppBasePath };

Expand Down Expand Up @@ -66,7 +67,8 @@ public AppDomainOperationExecutor(
{ "projectDir", ProjectDirectory },
{ "rootNamespace", RootNamespace },
{ "language", Language },
{ "toolsVersion", ProductInfo.GetVersion() }
{ "toolsVersion", ProductInfo.GetVersion() },
{ "remainingArguments", RemainingArguments }
}
},
null,
Expand Down
Loading