8

我们在 (localdb)\v11.0 (Vstudio 2012) 上使用带有 EF5 的代码优先迁移,到目前为止一切都运行良好。

但是 - 今天我需要在几个表上创建几个索引并遇到问题。

首先,我在 PM 中这样做:

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

我将脚手架迁移中的代码修改为:

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 控制台的标准输出结果:

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 方法并在数据库中成功更新该任务。但是当我添加一个新的迁移步骤时,它抱怨刚刚“提交”到数据库的任务仍然“待处理”——即使迁移历史和数据库对象都已更新。

4

3 回答 3

8

这可能无法解决 OP 问题,但是当我开发新数据库并尝试在对数据库进行任何更新之前添加两个迁移时,我遇到了类似的问题。

我的解决方案是运行我的应用程序,以便待处理的迁移可以更新数据库。应用程序在MigrateDatabaseToLatestVersion. 这也可以通过Update-Database从包管理器控制台键入来完成。

如果添加了迁移并且进行了更多更改,这些更改应该添加到同一个迁移中,那么也不需要删除它并重新添加它(我最初尝试这样做)。您可以只更新现有的待处理迁移,从包管理器控制台运行添加迁移工具时会显示帮助(我强调)。

此迁移文件的设计器代码包含当前 Code First 模型的快照。此快照用于在您构建下一次迁移时计算对模型的更改。如果您对要包含在此迁移中的模型进行其他更改,则可以通过再次运行“Add-Migration [Timestamp]_MigrationName”来重新构建它

换句话说,运行与待处理操作具有相同Id的Add-Migration工具,新的更改将与旧的合并。如果您注意到更改未合并,您可以使用-F标志(如 中Add-Migration <Name> -Force)来强制迁移,如注释中的 anthony-arnold 注释。

于 2014-01-30T10:03:21.300 回答
4

另一个可能的原因是 - 如果您在解决方案中有不同的项目,例如 - UI 和 Core(您的数据库配置在这里),那么请确保在运行迁移时您的 Core 项目设置为启动项目。它解决了我的问题。快乐编码:)

于 2015-06-11T13:14:17.560 回答
2

万一有人感兴趣,我在更改Configuration.cs 文件中的ContextKey值后遇到了“无法生成显式迁移,因为以下显式迁移正在等待...”错误。我把它改回来了,问题就消失了。

于 2015-05-22T14:55:30.600 回答