簡體   English   中英

無法生成顯式遷移(EF5)(遷移待處理)

[英]Unable to generate explicit migration (EF5) (migrations pending)

我們在(localdb)\\ v11.0(Vstudio 2012)上使用EF5進行代碼優先遷移,到目前為止一切運行良好。

但是 - 今天我需要在幾個表上創建幾個索引並遇到問題。

首先我在PM做了這個:

PM> add-migration AddIdxToOutage
Scaffolding migration 'AddIdxToOutage'.

我將scaffolded遷移中的代碼修改為:

public override void Up()
        {
            Sql(@"CREATE NONCLUSTERED INDEX [idx_WtgId_StartDateTime_EndDateTime] ON [dbo].[Outages]
(
    [WtgId] ASC,
    [StartDateTime] ASC,
    [EndDateTime] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]");
        }

我更新了數據庫,結果如下:

PM> update-database -startupprojectname D3A.Data -force -verbose
Using StartUp project 'D3A.Data'.
Using NuGet project 'D3A.Data'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'D3A.Data.StorageContext' (DataSource: (localdb)\v11.0, Provider: System.Data.SqlClient, Origin: Convention).
Applying code-based migrations: [201310301258520_AddIdxToOutage].
Applying code-based migration: 201310301258520_AddIdxToOutage.
CREATE NONCLUSTERED INDEX [idx_WtgId_StartDateTime_EndDateTime] ON [dbo].[Outages]
(
    [WtgId] ASC,
    [StartDateTime] ASC,
    [EndDateTime] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
[Inserting migration history record]
Running Seed method.

idx是在桌子上創建的,每個人都很高興。

但是 - 當我創建下一個索引時,我遇到了問題。 我創建了一個空遷移任務

PM> add-migration AddIdxToState
Unable to generate an explicit migration because the following explicit migrations are pending: [201310301258520_AddIdxToOutage]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

請原諒我的法語 - 但WT *?

似乎沒有辦法解決這個問題。 我可以在添加第一個idx之前恢復到遷移步驟,再次添加idx'es並再次發生同樣的事情。

你能告訴我這里缺少什么嗎? 我究竟做錯了什么?

編輯:

我最初認為我的麻煩是對數據庫/ localdb執行原始SQL,但似乎我現在所做的一切都在第一次添加遷移后停止。

我剛剛在數據庫中添加了一個新表,這是PM控制台的std-out結果:

PM> add-migration AddMyLoggerTable
Scaffolding migration 'AddMyLoggerTable'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration 201310301419063_AddMyLoggerTable' again.
PM> update-database -startupproject DongEnergy.D3A.Data -verbose
Using StartUp project 'D3A.Data'.
Using NuGet project 'D3A.Data'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'D3A.Data.StorageContext' (DataSource: (localdb)\v11.0, Provider: System.Data.SqlClient, Origin: Convention).
Applying code-based migrations: [201310301419063_AddMyLoggerTable].
Applying code-based migration: 201310301419063_AddMyLoggerTable.
CREATE TABLE [dbo].[MyLoggerTable] (
    [id] [int] NOT NULL,
    [functionId] [int],
    [elapsedTime] [bigint],
    [noOfRecords] [int],
    [dateCreated] [datetime] DEFAULT getDate()
)
[Inserting migration history record]
Running Seed method.
PM> add-migration AddMyLoggerTableFunction
Unable to generate an explicit migration because the following explicit migrations are pending: [201310301419063_AddMyLoggerTable]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

請注意我如何添加新的空遷移任務,使用CreateTable方法並在數據庫中成功更新。 但是當我添加一個新的遷移步驟時,它會抱怨剛剛“提交”到數據庫的任務仍處於“待定”狀態 - 即使已經更新了migrationhistory和數據庫對象。

這可能無法解決OP問題,但在開發新數據庫時我遇到了類似的問題,並嘗試在對數據庫進行任何更新之前添加兩個遷移。

我的解決方案是運行我的應用程序,以便掛起的遷移可以更新數據庫。 應用程序在MigrateDatabaseToLatestVersion的幫助下自動更新數據庫。 這可以通過從Package Manager控制台鍵入Update-Database來完成。

如果添加了遷移並且進行了更多應該添加到同一遷移的更改,則也無需刪除它並重新添加(我最初嘗試這樣做)。 您只需更新現有的掛起遷移,從Package Manager控制台運行Add-Migration工具時會顯示幫助(由我強調)。

此遷移文件的Designer代碼包含當前Code First模型的快照。 此快照用於在您構建下一次遷移時計算模型的更改。 如果對要在此遷移中包含的模型進行其他更改 ,則可以通過再次運行“添加 - 遷移[時間戳] _MigrationName”來重新構建它

換句話說,使用與掛起操作相同的Id運行Add-Migration工具,新的更改將與舊的合並。 如果您注意到未更改更改,則可以使用-F標志(如Add-Migration <Name> -Force )強制遷移,如評論中的anthony-arnold所述。

另一個可能的原因是 - 如果您在解決方案中有不同的項目,如UI和Core(您的數據庫配置在這里),那么請確保在運行遷移時將Core項目設置為啟動項目。 它解決了我的問題。 快樂編碼:)

只是因為任何人都感興趣我在Configuration.cs文件中更改了ContextKey值之后遇到了“無法生成顯式遷移,因為以下顯式遷移正在等待......”錯誤。 我改回來了,問題就消失了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM