diff --git a/src/EFCore.Design/Design/Internal/DbContextOperations.cs b/src/EFCore.Design/Design/Internal/DbContextOperations.cs index 78a3bcb808d..11997ee8200 100644 --- a/src/EFCore.Design/Design/Internal/DbContextOperations.cs +++ b/src/EFCore.Design/Design/Internal/DbContextOperations.cs @@ -102,6 +102,11 @@ protected DbContextOperations( /// public virtual void DropDatabase(string? contextType, string? connectionString) { + if (contextType == "*") + { + throw new OperationException(DesignStrings.WildcardNotSupported); + } + using var context = CreateContext(contextType); if (connectionString != null) @@ -425,6 +430,11 @@ private IReadOnlyList PrecompileQueries( /// public virtual ContextInfo GetContextInfo(string? contextType, string? connectionString = null) { + if (contextType == "*") + { + throw new OperationException(DesignStrings.WildcardNotSupported); + } + using var context = CreateContext(contextType); if (connectionString != null) diff --git a/src/EFCore.Design/Design/Internal/MigrationsOperations.cs b/src/EFCore.Design/Design/Internal/MigrationsOperations.cs index 1410f4b875d..db89d8a69e9 100644 --- a/src/EFCore.Design/Design/Internal/MigrationsOperations.cs +++ b/src/EFCore.Design/Design/Internal/MigrationsOperations.cs @@ -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); @@ -147,6 +152,11 @@ public virtual IEnumerable GetMigrations( string? connectionString, bool noConnect) { + if (contextType == "*") + { + throw new OperationException(DesignStrings.WildcardNotSupported); + } + using var context = _contextOperations.CreateContext(contextType); if (connectionString != null) @@ -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); @@ -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); + } } - var services = _servicesBuilder.Build(context); - EnsureServices(services); + _reporter.WriteInformation(DesignStrings.Done); + return; + } - var migrator = services.GetRequiredService(); - 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(); + migrator.Migrate(targetMigration); + } + /// /// 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 @@ -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) diff --git a/src/EFCore.Design/Properties/DesignStrings.Designer.cs b/src/EFCore.Design/Properties/DesignStrings.Designer.cs index 140351da3f2..caaac6bc315 100644 --- a/src/EFCore.Design/Properties/DesignStrings.Designer.cs +++ b/src/EFCore.Design/Properties/DesignStrings.Designer.cs @@ -979,6 +979,12 @@ public static string MigrationCreatedAndApplied(object? migrationId) GetString("MigrationCreatedAndApplied", nameof(migrationId)), migrationId); + /// + /// The wildcard '*' is not supported for this command. + /// + public static string WildcardNotSupported + => GetString("WildcardNotSupported"); + private static string GetString(string name, params string[] formatterNames) { var value = _resourceManager.GetString(name)!; diff --git a/src/EFCore.Design/Properties/DesignStrings.resx b/src/EFCore.Design/Properties/DesignStrings.resx index f167b4fb0da..3ce3000d05f 100644 --- a/src/EFCore.Design/Properties/DesignStrings.resx +++ b/src/EFCore.Design/Properties/DesignStrings.resx @@ -512,4 +512,7 @@ Change your target project to the migrations project by using the Package Manage Migration '{migrationId}' was successfully created and applied. + + The wildcard '*' can only be used with commands that run for all contexts found. Specify a context name for this command. + \ No newline at end of file diff --git a/src/ef/Commands/MigrationsBundleCommand.cs b/src/ef/Commands/MigrationsBundleCommand.cs index 6f69298b11c..4961fed1bff 100644 --- a/src/ef/Commands/MigrationsBundleCommand.cs +++ b/src/ef/Commands/MigrationsBundleCommand.cs @@ -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"]!; } diff --git a/src/ef/Properties/Resources.Designer.cs b/src/ef/Properties/Resources.Designer.cs index 50a5257a3af..2cf6e3d7fb0 100644 --- a/src/ef/Properties/Resources.Designer.cs +++ b/src/ef/Properties/Resources.Designer.cs @@ -725,6 +725,12 @@ public static string WritingFile(object? file) GetString("WritingFile", nameof(file)), file); + /// + /// The wildcard '*' is not supported for this command. + /// + public static string WildcardNotSupported + => GetString("WildcardNotSupported"); + private static string GetString(string name, params string[] formatterNames) { var value = _resourceManager.GetString(name)!; diff --git a/src/ef/Properties/Resources.resx b/src/ef/Properties/Resources.resx index 86f219db094..eddeede5702 100644 --- a/src/ef/Properties/Resources.resx +++ b/src/ef/Properties/Resources.resx @@ -441,4 +441,7 @@ Writing '{file}'... + + The wildcard '*' is not supported for this command. +