Database applies ALL previous migrations on update and NOT just the new one

database database-migration ef-migrations entity-framework entity-framework-6


I'm developing a website which, as of current, both has a production and a test database. The production database is hosted externally while the test database is hosted locally.

Whenever I make changes to my database I apply the changes through a migration. After having added a new migration I run the update-database command on both my production and test database to keep them in sync.

I applied the migration just fine to my production database, however, when I wanna apply the migration to my test database I see that it attempts to apply ALL the previous migrations (and not just the new one):

Here is the output:

Applying explicit migrations: [201603230047093_Initial, 201603232305269_AddedBlobNameToImage, 201603242121190_RemovedSourceFromRealestateDbTable, 201603311617077_AddedSourceUrlId, 201604012033331_AddedIndexProfileAndFacebookNotifications, 201604012233271_RemovedTenantIndexProfile, 201604042359214_AddRealestateFilter]. Applying explicit migration: 201603230047093_Initial. System.Data.SqlClient.SqlException (0x80131904): There is already an object named 'Cities' in the database.

Obviously it fails since the current state of the database is at the second latest migration. However I wonder why it attempts to apply ALL the previous migrations?

Unlike the production database (which has had all the migrations applied one at a time), the test database was deleted and created at the previous migration so its migration history table only contains one row:


(I assume InitialCreate is an auto generated name of all the previous migrations combined).

In summary:

Why is the test database trying to apply ALL the previous migrations instead of just the newly added?

EDIT: When running COMMMAND I get the follow output script:

DECLARE @CurrentMigration [nvarchar](max)

IF object_id('[dbo].[__MigrationHistory]') IS NOT NULL
    SELECT @CurrentMigration =
        (SELECT TOP (1) 
        [Project1].[MigrationId] AS [MigrationId]
        FROM ( SELECT 
        [Extent1].[MigrationId] AS [MigrationId]
        FROM [dbo].[__MigrationHistory] AS [Extent1]
        WHERE [Extent1].[ContextKey] = N'Boligside.Migrations.Configuration'
        )  AS [Project1]
        ORDER BY [Project1].[MigrationId] DESC)

IF @CurrentMigration IS NULL
    SET @CurrentMigration = '0'

IF @CurrentMigration < '201603230047093_Initial'

(it proceeds making if statements for each previous migration)

The current migrations table in my database looks the following (note that the first row is for a logging framework so it's not related): enter image description here

4/6/2016 6:33:24 PM

Accepted Answer

One issue that can cause migrations to rerun is if your context key changes which can happen during refactoring. There are a couple of ways to solve this:

1) Update the old records in __MigrationHistory with the new values:

UPDATE [dbo].[__MigrationHistory]
   SET [ContextKey] = ‘New_Namespace.Migrations.Configuration’
 WHERE [ContextKey] = ‘Old_Namespace.Migrations.Configuration’

2) You can hard code the old context key into the constructor of your migration Configuration class:

public Configuration()
    AutomaticMigrationsEnabled = false;
    this.ContextKey = “Old_Namespace.Migrations.Configuration”;

Here is a good article on how migrations run under the hood:

See also

4/6/2016 8:15:02 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow