Skip to content

Commit

Permalink
Allow creation of a DbMigrator with an explicit DbContext instance
Browse files Browse the repository at this point in the history
This prevents migrations having to attempt to create connections from connection strings.

Fixes #522
  • Loading branch information
ajcvickers committed Aug 16, 2019
1 parent 54641fb commit 025d974
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/EntityFramework/Migrations/DbMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,17 @@ public DbMigrator(DbMigrationsConfiguration configuration)
Check.NotNull(configuration.ContextType, "configuration.ContextType");
}

/// <summary>
/// Initializes a new instance of the DbMigrator class using the supplied context.
/// Use this constructor when applying migrations from code to avoid having migrations attempt
/// to create a context/connection for you.
/// </summary>
/// <param name="configuration"> Configuration to be used for the migration process. </param>
/// <param name="context"> The <see cref="DbContext"/> to use. </param>
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
[SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
internal DbMigrator(DbMigrationsConfiguration configuration, DbContext usersContext)
: this(configuration, usersContext, DatabaseExistenceState.Unknown, calledByCreateDatabase: false)
public DbMigrator(DbMigrationsConfiguration configuration, DbContext context)
: this(configuration, context, DatabaseExistenceState.Unknown, calledByCreateDatabase: false)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace System.Data.Entity.Migrations
{
using System.Data.Common;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations.Design;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using SimpleModel;
using Xunit;

public class NoTestInfraScenarios : TestBase
Expand All @@ -33,6 +36,41 @@ var migrator
Assert.False(string.IsNullOrWhiteSpace(migration.Directory));
}

[Fact]
public void Can_generate_migration_from_user_code_without_cloning_connection()
{
var connectionString = SimpleConnectionString("Db471a");

using (var context = new EmptyContext(connectionString))
{
context.Database.Delete();
}

using (var context = new Context471(connectionString))
{
var migrationConfiguration = new Configuration471(connectionString);
var migrator = new DbMigrator(migrationConfiguration, context);

var connectionField = migrator.GetType().GetField(
"_connection",
BindingFlags.Instance | BindingFlags.NonPublic);

Assert.Same(context.Database.Connection, connectionField.GetValue(migrator));

migrator.Update();

Assert.Same(context.Database.Connection, connectionField.GetValue(migrator));

migrator = new DbMigrator(migrationConfiguration, context);

Assert.Same(context.Database.Connection, connectionField.GetValue(migrator));

migrator.Update();

Assert.Same(context.Database.Connection, connectionField.GetValue(migrator));
}
}

[Fact] // CodePlex #2579
public void Repro()
{
Expand Down Expand Up @@ -97,6 +135,49 @@ public Context2579(string nameOrConnectionString)
public DbSet<Entity2579> Entities { get; set; }
}

public class Context471 : DbContext
{
static Context471()
{
Database.SetInitializer<Context471>(null);
}

public Context471()
{
}

public Context471(string nameOrConnectionString)
: base(CreateConnection(nameOrConnectionString), true)
{
}

private static DbConnection CreateConnection(string connectionString)
{
return new SqlConnection(connectionString);
}

public DbSet<Entity471> Entities { get; set; }
}

public class Entity471
{
public int Id { get; set; }
}

internal sealed class Configuration471 : DbMigrationsConfiguration<Context471>
{
public Configuration471()
{
AutomaticMigrationsEnabled = true;
}

public Configuration471(string connectionString)
{
AutomaticMigrationsEnabled = true;
TargetDatabase = new DbConnectionInfo(connectionString, "System.Data.SqlClient");
}
}

public class Context2579I : DbContext
{
static Context2579I()
Expand Down

0 comments on commit 025d974

Please sign in to comment.