entity-framework - 无法生成显式迁移 (EF5)(迁移待定)

标签 entity-framework indexing entity-framework-migrations localdb

我们在 (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 被创建在 table 上,每个人都很高兴。

但是 - 当我创建下一个索引时,我遇到了问题。我创建了一个空的迁移任务
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,但似乎我现在所做的一切在第一次添加迁移后都停止了。

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

最佳答案

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

我的解决方案是运行我的应用程序,以便挂起的迁移可以更新数据库。应用程序在 MigrateDatabaseToLatestVersion 的帮助下自动更新数据库.这可以通过键入 Update-Database 来完成。从包管理器控制台也是如此。

如果添加了迁移并进行了更多更改,这些更改应该添加到同一个迁移中,则也无需删除它并重新添加它(我最初尝试这样做)。您可以只更新现有的挂起迁移,从包管理器控制台运行 Add-Migration 工具时会显示帮助(我强调)。

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 [Timestamp]_MigrationName' again.



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

关于entity-framework - 无法生成显式迁移 (EF5)(迁移待定),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19683198/

相关文章:

c# - Entity Framework 是否支持模型中的不同数据类型?

python - 列表索引 - 如果太大则返回到列表的开头

ef-code-first - 添加迁移在代码迁移/代码优先中创建空迁移

linq - EF - 无法从 System.Linq.IQueryable 转换为 System.Data.Objects.ObjectQuery

entity-framework - 使用 EF6 和 MS Identity 的示例项目

c# - 如何在 MVC 中实现所有 sql 表的搜索功能?

sql - PostgreSQL 中 varchar 列的索引长度

Mongodb 查询不在带有文本字段的复合索引上使用前缀

entity-framework - 如何使用 EF Code First 将一个表关联到多个父项

data-annotations - 等效于 .WillCascadeOnDelete(false) 的 Entity Framework 数据注释;