简体   繁体   English

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

[英]Unable to generate explicit migration (EF5) (migrations pending)

We are using Code-first migrations with EF5 on (localdb)\\v11.0 (Vstudio 2012) and everything has worked nicely so far. 我们在(localdb)\\ v11.0(Vstudio 2012)上使用EF5进行代码优先迁移,到目前为止一切运行良好。

However - today I needed to create a couple of indexes on a few of tables and ran into problems. 但是 - 今天我需要在几个表上创建几个索引并遇到问题。

First I did this in PM: 首先我在PM做了这个:

PM> add-migration AddIdxToOutage
Scaffolding migration 'AddIdxToOutage'.

I modified the code in the scaffolded migration to: 我将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]");
        }

I updated the database and the result was this: 我更新了数据库,结果如下:

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.

The idx was created on the table and everybody was happy. idx是在桌子上创建的,每个人都很高兴。

However - when I created the next index, I ran into problems. 但是 - 当我创建下一个索引时,我遇到了问题。 I created an empty migration task as 我创建了一个空迁移任务

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.

Pardon my French - but WT*? 请原谅我的法语 - 但WT *?

There seems to be no way around this. 似乎没有办法解决这个问题。 I can revert to the migration step preceding addition of the first idx, add the idx'es again and the same thing happens again. 我可以在添加第一个idx之前恢复到迁移步骤,再次添加idx'es并再次发生同样的事情。

Can you tell me what I am missing here? 你能告诉我这里缺少什么吗? What am I doing wrong? 我究竟做错了什么?

Edit: 编辑:

I initially thought that my trouble was executing raw SQL against the database/localdb, but it seems like everything I do now stops after the first add-migration. 我最初认为我的麻烦是对数据库/ localdb执行原始SQL,但似乎我现在所做的一切都在第一次添加迁移后停止。

I just added a new table to the database and this is the std-out result from PM Console: 我刚刚在数据库中添加了一个新表,这是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.

Notice how I add a new, empty migration task, use the CreateTable-method and have that successfully updated in the database. 请注意我如何添加新的空迁移任务,使用CreateTable方法并在数据库中成功更新。 But when I add a new migration step, it complains that the task that was just "committed" to the database is still "pending" - even though both migrationhistory and database objects have been updated. 但是当我添加一个新的迁移步骤时,它会抱怨刚刚“提交”到数据库的任务仍处于“待定”状态 - 即使已经更新了migrationhistory和数据库对象。

This might not solve the OP question, but I had a similar issue when I was developing a new database and tried to add two migrations before doing any update to the database. 这可能无法解决OP问题,但在开发新数据库时我遇到了类似的问题,并尝试在对数据库进行任何更新之前添加两个迁移。

My solution was to run my application so the pending migration(s) could update the database. 我的解决方案是运行我的应用程序,以便挂起的迁移可以更新数据库。 The application updates the database automatically with the help of a MigrateDatabaseToLatestVersion . 应用程序在MigrateDatabaseToLatestVersion的帮助下自动更新数据库。 This can be accomplished by typing Update-Database from the Package Manager Console as well. 这可以通过从Package Manager控制台键入Update-Database来完成。

If a migration is added and more changes are made which should be added to the same migration, there's also no need to remove it and re-add it (which I tried to do at first). 如果添加了迁移并且进行了更多应该添加到同一迁移的更改,则也无需删除它并重新添加(我最初尝试这样做)。 You can just update the existing pending migration, the help is displayed when running the Add-Migration tool from the Package Manager Console (emphasis by me). 您只需更新现有的挂起迁移,从Package Manager控制台运行Add-Migration工具时会显示帮助(由我强调)。

The Designer Code for this migration file includes a snapshot of your current Code First model. 此迁移文件的Designer代码包含当前Code First模型的快照。 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 [Timestamp]_MigrationName' again . 如果对要在此迁移中包含的模型进行其他更改 ,则可以通过再次运行“添加 - 迁移[时间戳] _MigrationName”来重新构建它

In other words, run the Add-Migration tool with the same Id as the pending operation, and the new changes will be merged with the old. 换句话说,使用与挂起操作相同的Id运行Add-Migration工具,新的更改将与旧的合并。 If you notice that the changes are not merged, you can use the -F flag (as in Add-Migration <Name> -Force ) to force the migration, as anthony-arnold notes in the comments. 如果您注意到未更改更改,则可以使用-F标志(如Add-Migration <Name> -Force )强制迁移,如评论中的anthony-arnold所述。

Another possible reason is - if you have different projects in the solution like - UI and Core (your db configuration is here), then make sure while running the migration your Core project is set as Startup project. 另一个可能的原因是 - 如果您在解决方案中有不同的项目,如UI和Core(您的数据库配置在这里),那么请确保在运行迁移时将Core项目设置为启动项目。 It solved my problem. 它解决了我的问题。 Happy coding :) 快乐编码:)

Just is case anybody is interested I ran into the "Unable to generate an explicit migration because the following explicit migrations are pending..." error after I changed the ContextKey value in the Configuration.cs file. 只是因为任何人都感兴趣我在Configuration.cs文件中更改了ContextKey值之后遇到了“无法生成显式迁移,因为以下显式迁移正在等待......”错误。 I changed it back and the issue went away. 我改回来了,问题就消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法生成显式迁移,因为以下显式迁移待处理 - Unable to generate an explicit migration because the following explicit migrations are pending 实体框架迁移问题。 [无法生成显式迁移,因为以下显式迁移正在处理中] - Issue with entity framework migration. [Unable to generate an explicit migration because the following explicit migrations are pending] EF Add-Migration表示“没有待定的显式迁移”,但Update-Database会抱怨“..有待处理的更改” - EF Add-Migration indicates “No pending explicit migrations” but Update-Database complains “..there are pending changes” 在显式迁移挂起时添加迁移 - Add-Migration while there are explicit migrations pending EF5代码优先迁移会重置迁移 - EF5 Code first migrations reset migrations 实体框架:将迁移类移动到迁移文件夹后,从挂起的显式迁移列表中丢失迁移类 - Entity Framework: Migration classes missing from list of pending explicit migrations after moving them to the migrations folder 无法在实体框架中生成显式迁移 - Unable to generate an explicit migration in entity framework EF5-&gt; EF6奇怪的迁移行为 - EF5 -> EF6 strange migration behavior EF5数据库迁移:如何移动数据 - EF5 Database Migrations: How to move data EF5代码首先有许多人迁移 - EF5 Code First Many to Many with Migrations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM