Skip to content
Open
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
10 changes: 10 additions & 0 deletions src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ protected DbContextOperations(
/// </summary>
public virtual void DropDatabase(string? contextType, string? connectionString)
{
if (contextType == "*")
{
throw new OperationException(DesignStrings.WildcardNotSupported);
}

using var context = CreateContext(contextType);

if (connectionString != null)
Expand Down Expand Up @@ -425,6 +430,11 @@ private IReadOnlyList<string> PrecompileQueries(
/// </summary>
public virtual ContextInfo GetContextInfo(string? contextType, string? connectionString = null)
{
if (contextType == "*")
{
throw new OperationException(DesignStrings.WildcardNotSupported);
Comment on lines +433 to +435
}

using var context = CreateContext(contextType);

if (connectionString != null)
Expand Down
60 changes: 53 additions & 7 deletions src/EFCore.Design/Design/Internal/MigrationsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public virtual MigrationFiles AddMigration(
string? @namespace,
bool dryRun)
{
if (contextType == "*")
{
throw new OperationException(DesignStrings.WildcardNotSupported);
}

using var context = _contextOperations.CreateContext(contextType);
var services = PrepareForMigration(name, context);

Expand Down Expand Up @@ -147,6 +152,11 @@ public virtual IEnumerable<MigrationInfo> GetMigrations(
string? connectionString,
bool noConnect)
{
if (contextType == "*")
{
throw new OperationException(DesignStrings.WildcardNotSupported);
}

using var context = _contextOperations.CreateContext(contextType);

if (connectionString != null)
Expand Down Expand Up @@ -197,6 +207,11 @@ public virtual string ScriptMigration(
MigrationsSqlGenerationOptions options,
string? contextType)
{
if (contextType == "*")
{
throw new OperationException(DesignStrings.WildcardNotSupported);
}

using var context = _contextOperations.CreateContext(contextType);
var services = _servicesBuilder.Build(context);
EnsureServices(services);
Expand All @@ -217,23 +232,49 @@ public virtual void UpdateDatabase(
string? connectionString,
string? contextType)
{
using (var context = _contextOperations.CreateContext(contextType))
if (contextType == "*")
{
if (connectionString != null)
var contexts = _contextOperations.CreateAllContexts();

if (!contexts.Any())
{
throw new OperationException(DesignStrings.NoContext(_assembly.GetName().Name));
}

foreach (var item in contexts)
{
context.Database.SetConnectionString(connectionString);
using (item)
{
MigrateContext(item, targetMigration, connectionString);
Comment thread
AndriySvyryd marked this conversation as resolved.
}
Comment thread
AndriySvyryd marked this conversation as resolved.
}

var services = _servicesBuilder.Build(context);
EnsureServices(services);
_reporter.WriteInformation(DesignStrings.Done);
return;
}

var migrator = services.GetRequiredService<IMigrator>();
migrator.Migrate(targetMigration);
using (var context = _contextOperations.CreateContext(contextType))
{
MigrateContext(context, targetMigration, connectionString);
}

_reporter.WriteInformation(DesignStrings.Done);
}

private void MigrateContext(DbContext context, string? targetMigration, string? connectionString)
{
if (connectionString is not null)
{
context.Database.SetConnectionString(connectionString);
}

var services = _servicesBuilder.Build(context);
EnsureServices(services);

var migrator = services.GetRequiredService<IMigrator>();
migrator.Migrate(targetMigration);
}

/// <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 All @@ -247,6 +288,11 @@ public virtual MigrationFiles RemoveMigration(
bool dryRun,
string? connectionString)
{
if (contextType == "*")
{
throw new OperationException(DesignStrings.WildcardNotSupported);
}

using var context = _contextOperations.CreateContext(contextType);

if (connectionString != null)
Expand Down
6 changes: 6 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.

3 changes: 3 additions & 0 deletions src/EFCore.Design/Properties/DesignStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,7 @@ Change your target project to the migrations project by using the Package Manage
<data name="MigrationCreatedAndApplied" xml:space="preserve">
<value>Migration '{migrationId}' was successfully created and applied.</value>
</data>
<data name="WildcardNotSupported" xml:space="preserve">
<value>The wildcard '*' can only be used with commands that run for all contexts found. Specify a context name for this command.</value>
</data>
</root>
5 changes: 5 additions & 0 deletions src/ef/Commands/MigrationsBundleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ protected override int Execute(string[] args)
throw new CommandException(Resources.VersionRequired("6.0.0"));
}

if (Context!.Value() == "*")
{
throw new CommandException(Resources.WildcardNotSupported);
}

context = (string)executor.GetContextInfo(Context!.Value())["Type"]!;
}

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 @@ -441,4 +441,7 @@
<data name="WritingFile" xml:space="preserve">
<value>Writing '{file}'...</value>
</data>
<data name="WildcardNotSupported" xml:space="preserve">
<value>The wildcard '*' is not supported for this command.</value>
</data>
</root>