我们使用的是代码优先迁移(localdb)\v11.0 (Vstudio 2012),到目前为止一切都很好。
然而,今天我需要在几个表上创建几个索引,结果遇到了问题。
首先,我在下午做了这个:
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控制台的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-方法并在数据库中成功地更新该任务。但是当我添加一个新的迁移步骤时,它会抱怨刚刚“提交”到数据库的任务仍然是“未决”的--尽管迁移历史和数据库对象都已经更新了。
发布于 2014-01-30 10:03:21
这可能解决不了OP问题,但我在开发新数据库时遇到了类似的问题,在对数据库进行任何更新之前,我尝试添加两个迁移。
我的解决方案是运行我的应用程序,以便挂起的迁移可以更新数据库。应用程序在MigrateDatabaseToLatestVersion的帮助下自动更新数据库。这也可以通过从Package控制台输入Update-Database来实现。
如果添加了一个迁移,并且进行了更多的更改,应该将其添加到同一个迁移中,那么也没有必要删除它并重新添加它(我一开始尝试这样做)。您只需更新现有的挂起的迁移,在从Package控制台运行时就会显示该帮助(由我强调)。
此迁移文件的设计器代码包含当前代码第一模型的快照。此快照用于计算下一次迁移时对模型的更改。如果对您想要包含在迁移中的模型进行额外的更改,那么您可以通过运行‘Ad-MigrationTimestamp_MigrationName’再运行来重新构建它。
换句话说,使用与挂起操作相同的Id运行添加迁移工具,新的更改将与旧的合并。如果您注意到这些更改没有合并,可以使用-F标志(如Add-Migration <Name> -Force中的)强制迁移,正如anthony-arnold在注释中所指出的那样。
发布于 2015-06-11 13:14:17
另一个可能的原因是--如果解决方案中有不同的项目,如- UI和Core (您的db配置在这里),那么在运行迁移时确保将Core项目设置为Startup项目。它解决了我的问题。快乐编码:)
发布于 2015-05-22 14:55:30
如果有人感兴趣的话,我会遇到“无法生成显式迁移,因为下面的显式迁移正在等待.”更改ContextKey值后,Configuration.cs文件中出现错误。我把它改回来了,问题就解决了。
https://stackoverflow.com/questions/19683198
复制相似问题