Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creation of a DbMigrator with an explicit DbContext instance #1153

Merged
merged 1 commit into from
Aug 17, 2019
Merged
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
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