Skip to content
Closed
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
4 changes: 3 additions & 1 deletion src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,11 @@ private KeyValuePair<Type, Func<DbContext>> FindContextType(string? name)
Dictionary<Type, Func<DbContext>?> types,
string name,
StringComparison comparisonType)
=> types
{
return types
.Where(t => string.Equals(t.Key.Name, name, comparisonType)
|| string.Equals(t.Key.FullName, name, comparisonType)
|| string.Equals(t.Key.AssemblyQualifiedName, name, comparisonType))
.ToDictionary();
}
}
45 changes: 45 additions & 0 deletions src/EFCore.Design/Design/Internal/MigrationsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ public virtual MigrationFiles AddMigration(
string? @namespace,
bool dryRun)
{
if (contextType == "*")
{
var contexts = _contextOperations.CreateAllContexts();
List<MigrationFiles> filesList = new();

foreach(var item in contexts)
{
using (item)
{
var _service = PrepareForMigration(name, item);
using var _scope = _service.CreateScope();
var (_, _files) =
CreateScaffoldedMigration(name, outputDir, @namespace, dryRun, _scope.ServiceProvider);

filesList.Add(_files);
}
}

return filesList.Last();
}

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

Expand Down Expand Up @@ -217,6 +238,30 @@ public virtual void UpdateDatabase(
string? connectionString,
string? contextType)
{
if (contextType == "*")
{
var contexts = _contextOperations.CreateAllContexts();

foreach (var context in contexts)
{
using (context)
{
if (connectionString != null)
context.Database.SetConnectionString(connectionString);

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

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

_reporter.WriteInformation(DesignStrings.Done);

return;
}

using (var context = _contextOperations.CreateContext(contextType))
{
if (connectionString != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore.Internal;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using Microsoft.EntityFrameworkCore.Internal;

namespace Microsoft.EntityFrameworkCore.Design.Internal;
Expand Down Expand Up @@ -85,6 +86,45 @@ public void AddMigration_throws_when_name_is_whitespace()
Assert.Equal(DesignStrings.MigrationNameRequired, exception.Message);
}

[ConditionalFact]
public void UpdateDatabase_with_wildcard_runs_for_all_contexts()
{
var assembly = MockAssembly.Create(typeof(TestContext), typeof(AssemblyTestContext));
var reporter = new TestOperationReporter();
var operations = new TestMigrationsOperations(
reporter,
assembly,
assembly,
"projectDir",
"RootNamespace",
"C#",
nullable: false,
args: []);

operations.UpdateDatabase(null, null, "*");

Assert.DoesNotContain(reporter.Messages, m => m.Level == LogLevel.Error);
}

[ConditionalFact]
public void AddMigration_with_wildcard_runs_for_all_contexts()
{
var assembly = MockAssembly.Create(typeof(TestContext), typeof(AssemblyTestContext));
var operations = new TestMigrationsOperations(
new TestOperationReporter(),
assembly,
assembly,
"projectDir",
"RootNamespace",
"C#",
nullable: false,
args: []);

var result = operations.AddMigration("TestMigration", null, "*", null, dryRun: true);

Assert.NotNull(result);
}

private class TestContext : DbContext;

private class AssemblyTestContext : DbContext
Expand Down