4

I have Entity Framework (6.1.1) set up with migrations. I know I can run the following command in the Package Manager Console to reset the database to be completely empty:

Update-Database –TargetMigration: $InitialDatabase

But how can I do this from my code?

4
  • Can you explain a little bit about the use case surrounding this requirement? Are you trying to hook into the migrations system to trigger additional behavior? DbContext.Database.Delete(); DbContext.Database.Initialize(true); will drop and recreate the database but it is unclear if that is what you are after. Commented Jun 8, 2016 at 14:12
  • @Gusdor: To prevent a ton of migrations I have one migration per versioned release. Seeing as I'm going into alpha testing I might add something to the versioned migration after the first alpha release. When I release the next alpha version (or beta) there might be changes to the versioned migration already applied, so I have to reset the database to $InitialDatabase, otherwise the migration won't apply because EF thinks it is already applied, but there are mismatch between tables and migration. Commented Jun 8, 2016 at 14:59
  • @GTHvidsten did my apporach solve your problem? Commented Jun 8, 2016 at 15:19
  • @BassamAlugili I'll have a look first thing tomorrow morning. Commented Jun 8, 2016 at 17:06

1 Answer 1

3
        var configuration = new MyDbConfiguration();
        configuration.TargetDatabase = new DbConnectionInfo(
            "Server=MyServer;Database=MyDatabase;Trusted_Connection=True;",
            "System.Data.SqlClient");

        var migrator = new DbMigrator(configuration);
        migrator.Update("201606030938116_InitialDatabase");

if you do not know your migration Id then you can just do:

    migrator.GetDatabaseMigrations().First();

or:

    migrator.GetLocalMigrations().First();

In your DbConfiguration you have to allow the auto dropping:

public class MyDbConfiguration: DbMigrationsConfiguration<MyDbContext>
  {
    public MyDbConfiguration()
    {
      this.AutomaticMigrationsEnabled = true;
      this.AutomaticMigrationDataLossAllowed = true;
    }
  }

Update from @GTHvidsten:

Instead of getting the available migrations, you have to use this command: migrator.Update(DbMigrator.InitialDatabase);. But you also have to set the ContextKey property in MyDbConfiguration to match the one used in the Configuration object created by Package Manager. With both of these my database becomes empty.

Sign up to request clarification or add additional context in comments.

4 Comments

I can't get this to work. It finds the migrations, but when I try to do migrator.Update(migrator.GetLocalMigrations().First()) I get an error saying "There is already an object named 'SomeTable' in the database". The original Migration Configuration object (created through Package Manager) has set a value to the ContextKey property. I tried setting the same value in MyDbConfiguration. This causes the error to disappear, but nothing happens at the Update() call. The database remains exactly like before.
Okay, I think I got it to work. Instead of getting the available migrations, you have to use this command: migrator.Update(DbMigrator.InitialDatabase);. But you also have to set the ContextKey property in MyDbConfiguration to match the one used in the Configuration object created by Package Manager. With both of these my database becomes empty.
Good news ) Mark it as answer
I will. Maybe you could update your answer to reflect what I have found, so that other won't have to read the comments to get it to work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.