diff --git a/src/EFCore.Design/Design/Internal/DbContextOperations.cs b/src/EFCore.Design/Design/Internal/DbContextOperations.cs index 11997ee8200..21de633b66a 100644 --- a/src/EFCore.Design/Design/Internal/DbContextOperations.cs +++ b/src/EFCore.Design/Design/Internal/DbContextOperations.cs @@ -104,12 +104,33 @@ public virtual void DropDatabase(string? contextType, string? connectionString) { if (contextType == "*") { - throw new OperationException(DesignStrings.WildcardNotSupported); + var contexts = CreateAllContexts(); + + if (!contexts.Any()) + { + throw new OperationException(DesignStrings.NoContext(_assembly.GetName().Name)); + } + + foreach(var item in contexts) + { + using (item) + { + DropDatabaseContext(item, connectionString); + } + } + + return; } - using var context = CreateContext(contextType); + using (var context = CreateContext(contextType)) + { + DropDatabaseContext(context, connectionString); + } + } - if (connectionString != null) + private void DropDatabaseContext(DbContext context, string? connectionString) + { + if (connectionString is not null) { context.Database.SetConnectionString(connectionString); } diff --git a/src/EFCore.Design/Design/Internal/MigrationsOperations.cs b/src/EFCore.Design/Design/Internal/MigrationsOperations.cs index db89d8a69e9..012e80f6a10 100644 --- a/src/EFCore.Design/Design/Internal/MigrationsOperations.cs +++ b/src/EFCore.Design/Design/Internal/MigrationsOperations.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics.CodeAnalysis; +using System.Text; using Microsoft.EntityFrameworkCore.Internal; using Microsoft.EntityFrameworkCore.Migrations.Design; using Microsoft.EntityFrameworkCore.Migrations.Design.Internal; @@ -154,12 +155,36 @@ public virtual IEnumerable GetMigrations( { if (contextType == "*") { - throw new OperationException(DesignStrings.WildcardNotSupported); + var contexts = _contextOperations.CreateAllContexts(); + + if (!contexts.Any()) + { + throw new OperationException(DesignStrings.NoContext(_assembly.GetName().Name)); + } + + var contextsList = new List(); + foreach(var item in contexts) + { + using (item) + { + var migrationInfo = GetMigrationsContext(item, connectionString, noConnect); + contextsList.AddRange(migrationInfo); + } + } + + return contextsList; } - using var context = _contextOperations.CreateContext(contextType); + using (var context = _contextOperations.CreateContext(contextType)) + { + return GetMigrationsContext(context, connectionString, noConnect); + } + + } - if (connectionString != null) + private IEnumerable GetMigrationsContext(DbContext context, string? connectionString, bool noConnect) + { + if (connectionString is not null) { context.Database.SetConnectionString(connectionString); } @@ -209,10 +234,34 @@ public virtual string ScriptMigration( { if (contextType == "*") { - throw new OperationException(DesignStrings.WildcardNotSupported); + var contexts = _contextOperations.CreateAllContexts(); + + if (!contexts.Any()) + { + throw new OperationException(DesignStrings.NoContext(_assembly.GetName().Name)); + } + + var stringBuilder = new StringBuilder(); + foreach (var item in contexts) + { + using (item) + { + var script = ScriptMigrationContext(fromMigration, toMigration, options, item); + stringBuilder.Append(script); + } + } + + return stringBuilder.ToString(); } - using var context = _contextOperations.CreateContext(contextType); + using (var context = _contextOperations.CreateContext(contextType)) + { + return ScriptMigrationContext(fromMigration, toMigration, options, context); + } + } + + private string ScriptMigrationContext(string? fromMigration, string? toMigration, MigrationsSqlGenerationOptions options, DbContext context) + { var services = _servicesBuilder.Build(context); EnsureServices(services);