Entity framework 无法生成显式迁移(EF5)(迁移挂起)

Entity framework 无法生成显式迁移(EF5)(迁移挂起),entity-framework,indexing,entity-framework-migrations,localdb,Entity Framework,Indexing,Entity Framework Migrations,Localdb,我们正在(localdb)\v11.0(vstudio2012)上使用EF5的代码优先迁移,到目前为止一切都运行良好 然而,今天我需要在几个表上创建几个索引,结果遇到了问题 首先,我在下午做了这件事: PM> add-migration AddIdxToOutage Scaffolding migration 'AddIdxToOutage'. 我将脚手架迁移中的代码修改为: public override void Up() { Sql(@"CR

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

然而,今天我需要在几个表上创建几个索引,结果遇到了问题

首先,我在下午做了这件事:

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,同样的事情再次发生

你能告诉我我错过了什么吗?我做错了什么

编辑:

我最初认为我的问题是对数据库/localdb执行原始SQL,但似乎在第一次add迁移之后,我现在所做的一切都停止了

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

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

我的解决方案是运行我的应用程序,以便挂起的迁移可以更新数据库。该应用程序会在数据库的帮助下自动更新数据库。这也可以通过在Package Manager控制台中键入
Update Database
来实现

如果添加了一个迁移,并且进行了更多应该添加到同一迁移中的更改,那么也不需要删除它并重新添加它(这是我最初尝试的)。您可以只更新现有的挂起迁移,当从PackageManager控制台(emphasis by me)运行添加迁移工具时,将显示帮助

此迁移文件的设计器代码包含当前迁移文件的快照 代码优先模型。此快照用于计算对模型的更改 当您进行下一次迁移时如果您对您的应用程序进行其他更改 要包含在此迁移中的模型,然后可以重新构建它 通过再次运行“添加迁移[时间戳]\u MigrationName”


换句话说,使用与挂起操作相同的Id运行添加迁移工具,新更改将与旧更改合并。如果您注意到更改没有合并,您可以使用
-F
标志(如
添加迁移-Force
)强制迁移,正如anthony arnold在评论中指出的那样。

如果有人感兴趣,我会遇到“无法生成显式迁移,因为以下显式迁移正在挂起…”更改Configuration.cs文件中的ContextKey值后出错。我更改了它,问题消失了。

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

尝试:还原、删除迁移并重新创建。有几次我遇到了“空”迁移的问题,但这个简单的技巧总能解决它。@allo_-man,我试过这个。我返回到创建索引之前的迁移步骤。效果很好。然后我添加了创建索引的第一步。这工作得很好,更新数据库的工作也和预期的一样。当我添加迁移以创建第二个索引时,此操作失败。您的第一次迁移是否在_MigrationHistory表中创建了它的条目?请使用
-Force
,Luke。我的项目也是如此。我没有重新更改ContextKey,而是手动将其调整为数据库中_MigrationHistory表中的新值。这也解决了问题。