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
27 changes: 24 additions & 3 deletions src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
59 changes: 54 additions & 5 deletions src/EFCore.Design/Design/Internal/MigrationsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -154,12 +155,36 @@ public virtual IEnumerable<MigrationInfo> 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<MigrationInfo>();
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<MigrationInfo> GetMigrationsContext(DbContext context, string? connectionString, bool noConnect)
{
if (connectionString is not null)
{
context.Database.SetConnectionString(connectionString);
}
Expand Down Expand Up @@ -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);

Expand Down
Loading