数据库应用所有以前的迁移上更新,而不只是新的 [英] Database applies ALL previous migrations on update and NOT just the new one

查看:288
本文介绍了数据库应用所有以前的迁移上更新,而不只是新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网站,目前,它有一个生产和测试数据库。
生产数据库在外部托管,而测试数据库托管在本地。



每当我对数据库进行更改时,我通过迁移应用更改。
添加新迁移之后,我在我的生产和测试数据库上运行 update-database 命令,使它们保持同步。



我将迁移应用到我的生产数据库,但是,当我想将迁移应用到我的测试数据库时,我看到它尝试应用所有以前的迁移(而不只是新的迁移):



这是输出:


应用显式迁移:[201603230047093_Initial,
201603232305269_AddedBlobNameToImage,
201603242121190_RemovedSourceFromRealestateDbTable,
201603311617077_AddedSourceUrlId,
201604012033331_AddedIndexProfileAndFacebookNotifications,
201604012233271_RemovedTenantIndexProfile,
201604042359214_AddRealestateFilter]。应用显式迁移:
201603230047093_Initial。 System.Data.SqlClient.SqlException
(0x80131904):
数据库中已经有一个名为Cities的对象。


显然,它失败,因为数据库的当前状态是第二次最新的迁移。但是,我不知道为什么它试图应用所有以前的迁移?



与生产数据库(一次已应用一个迁移)不同,测试数据库删除和创建,因此其迁移历史记录表只包含一行:

  201604012239054_InitialCreate 

(假设InitialCreate是所有以前迁移的自动生成的名称)。



总结:



为什么测试数据库试图应用所有先前的迁移,而不仅仅是新添加的?



编辑:
当运行COMMMAND时,我得到以下输出脚本:

  DECLARE @CurrentMigration [nvarchar](max)

如果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)

如果@CurrentMigration IS NULL
SET @CurrentMigration ='0'

如果@CurrentMigration< '201603230047093_Initial'

(进行每个先前迁移的if语句)



我的数据库中的当前迁移表看起来如下(注意,第一行是一个日志框架,所以它不相关):



另请参阅 http://jameschambers.com/2014/02/changing-the-namespace-with-entity- framework-6-0-code-first-databases /


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):

解决方案

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/

这篇关于数据库应用所有以前的迁移上更新,而不只是新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆