Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

201604012239054_InitialCreate 

(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

like image 937
J. Doe Avatar asked Apr 06 '16 17:04

J. Doe


1 Answers

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: https://msdn.microsoft.com/en-US/data/dn481501?f=255&MSPPError=-2147217396

See also http://jameschambers.com/2014/02/changing-the-namespace-with-entity-framework-6-0-code-first-databases/

like image 56
Steve Greene Avatar answered Oct 22 '22 19:10

Steve Greene