无法生成显式迁移(EF5)(迁移待处理)

Jes*_*olm 8 indexing entity-framework ef-migrations localdb

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

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

首先我在PM做了这个:

PM> add-migration AddIdxToOutage
Scaffolding migration 'AddIdxToOutage'.
Run Code Online (Sandbox Code Playgroud)

我将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]");
        }
Run Code Online (Sandbox Code Playgroud)

我更新了数据库,结果如下:

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.
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

请原谅我的法语 - 但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.
Run Code Online (Sandbox Code Playgroud)

请注意我如何添加新的空迁移任务,使用CreateTable方法并在数据库中成功更新.但是当我添加一个新的迁移步骤时,它会抱怨刚刚"提交"到数据库的任务仍处于"待定"状态 - 即使已经更新了migrationhistory和数据库对象.

Pat*_*ick 8

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

我的解决方案是运行我的应用程序,以便挂起的迁移可以更新数据库.应用程序在a的帮助下自动更新数据库MigrateDatabaseToLatestVersion.这也可以通过Update-Database从Package Manager控制台输入来完成.

如果添加了迁移并且进行了更多应该添加到同一迁移的更改,则也无需删除它并重新添加(我最初尝试这样做).您只需更新现有的挂起迁移,从Package Manager控制台运行Add-Migration工具时会显示帮助(由我强调).

此迁移文件的Designer代码包含当前Code First模型的快照.此快照用于在您构建下一次迁移时计算模型的更改.如果对要在此迁移中包含的模型进行其他更改,则可以通过再次运行"添加 - 迁移[时间戳] _MigrationName"来重新构建它.

换句话说,使用与挂起操作相同的Id运行Add-Migration工具,新的更改将与旧的合并.如果您发现更改未合并,则可以使用-F标志(如Add-Migration <Name> -Force)来强制迁移,如评论中的anthony-arnold所述.

  • 使用`-Force`,Luke. (20认同)