2013-10-30 62 views
8

我们正在使用(的LocalDB)\ 11.0(Vstudio 2012)与EF5代码优先迁移和一切迄今很好的工作。无法产生明确的迁移(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'es,同样的事情再次发生。

你能告诉我我在这里失踪了什么吗?我究竟做错了什么?

编辑:

我最初以为我的麻烦正在执行针对数据库/生的LocalDB SQL,但似乎一切我现在做的第一附加迁移后停止。

我只是增加了一个新的表到数据库,这是从PM控制台STD-出结果:

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

+0

尝试:还原,删除您的迁移并重新创建它。我几次遇到了“空”迁移的问题,但这个简单的技巧总是做到了。 –

+0

@allo_man,好吧,我试过了。我恢复了迁移步骤/创建索引之前。这工作得很好。然后我添加了第一步创建索引。这工作得很好,更新数据库像预期的那样工作。当我添加迁移来创建第二个索引时,失败。 –

+0

您的第一次迁移是否在__MigrationHistory表中创建了条目? – Adrian

回答

8

这可能不是解决OP的问题,但我也有类似的问题,当我正在开发一个新的数据库,并试图做任何更新到数据库之前添加两个迁移。

我的解决办法是运行我的应用程序,以便未决迁移(S)可以更新数据库。应用程序会在MigrateDatabaseToLatestVersion的帮助下自动更新数据库。这可以通过从包管理器控制台输入Update-Database来完成。

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

此迁移文件的设计器代码包含您当前代码优先模型的当前快照 。此快照用于在您为下一次迁移脚手架时计算对模型 所做的更改。 如果您将更改为您要在此迁移中包含的 模型,然后,您可以通过再次运行“添加迁移[时间戳] _迁移名称”来重新搭建它 。

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

+20

使用'-Force',卢克。 – Anthony

2

恰恰是任何人的情况下有兴趣我跑进“无法因为以下明确迁移正等待产生一个明确的迁移......”之后错误我在Configuration.cs文件改变了ContextKey值。我把它改回来了,问题就消失了。

+1

我的项目也是如此。我没有手动将ContextKey更改为数据库中_MigrationHistory表中的新值。这也解决了问题。 –

4

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

+0

圣牛!为什么这个工作?谢谢Tonu。 – Jess