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