使用实体框架代码更改PK数据类型时出现的问题首次在Azure上进行迁移

时间:2014-05-02 15:54:00

标签: entity-framework azure azure-sql-database ef-migrations

我在Azure上托管了代码优先的EntityFramework项目。目前我通过

处理迁移
  1. 从程序包管理器控制台运行添加迁移
  2. 从Package Manager控制台运行Update-Database -script,获取.sql脚本
  3. 在我的开发环境中测试后,在Azure上手动运行脚本
  4. 到目前为止,这一直非常顺利。但是我遇到了一些严重的问题,试图将我的一个表的主键从int更改为long。

    以下是从EntityFramework

    生成的迁移脚本
    ALTER TABLE [ico].[AccountFeedbacks] ALTER COLUMN [GameId] [bigint]
    ALTER TABLE [ico].[Games] ALTER COLUMN [Id] [bigint] NOT NULL
    ALTER TABLE [ico].[Accounts] ALTER COLUMN [LastWarningGameId] [bigint]
    ALTER TABLE [ico].[AccountGameRecords] ALTER COLUMN [GameId] [bigint] NOT NULL
    ALTER TABLE [ico].[GameFeedbacks] ALTER COLUMN [GameId] [bigint] NOT NULL
    
    --__MigrationHistory excluded
    

    由于INDEX和FK CONSTRAINTS,运行此脚本根本不起作用。所以我首先手动更改脚本:

    DROP INDEX [IX_GameId] ON [ico].[GameFeedbacks]
    DROP INDEX [IX_GameId] ON [ico].[AccountGameRecords]
    DROP INDEX [IX_GameId] ON [ico].[AccountFeedbacks]
    DROP INDEX [IX_LastWarningGameId] ON [ico].[Accounts]
    ALTER TABLE [ico].[AccountFeedbacks] DROP CONSTRAINT [FK_ico.AccountFeedbacks_ico.Games_GameId]
    ALTER TABLE [ico].[Accounts] DROP CONSTRAINT [FK_ico.Accounts_ico.Games_LastWarningGameId]
    ALTER TABLE [ico].[GameFeedbacks] DROP CONSTRAINT [FK_ico.GameFeedbacks_ico.Games_GameId]
    ALTER TABLE [ico].[AccountGameRecords] DROP CONSTRAINT [FK_ico.AccountGameRecords_ico.Games_GameId]
    ALTER TABLE [ico].[Games] DROP CONSTRAINT [PK_ico.Games]
    
    ALTER TABLE [ico].[AccountFeedbacks] ALTER COLUMN [GameId] [bigint]
    ALTER TABLE [ico].[Games] ALTER COLUMN [Id] [bigint] NOT NULL
    ALTER TABLE [ico].[Accounts] ALTER COLUMN [LastWarningGameId] [bigint]
    ALTER TABLE [ico].[AccountGameRecords] ALTER COLUMN [GameId] [bigint] NOT NULL
    ALTER TABLE [ico].[GameFeedbacks] ALTER COLUMN [GameId] [bigint] NOT NULL
    
    ALTER TABLE [ico].[Games] ADD CONSTRAINT [PK_ico.Games] PRIMARY KEY ([Id])
    ALTER TABLE [ico].[AccountGameRecords] ADD CONSTRAINT [FK_ico.AccountGameRecords_ico.Games_GameId] FOREIGN KEY ([GameId]) REFERENCES [ico].[Games] ([Id]) ON DELETE CASCADE
    ALTER TABLE [ico].[GameFeedbacks] ADD CONSTRAINT [FK_ico.GameFeedbacks_ico.Games_GameId] FOREIGN KEY ([GameId]) REFERENCES [ico].[Games] ([Id]) ON DELETE CASCADE
    ALTER TABLE [ico].[Accounts] ADD CONSTRAINT [FK_ico.Accounts_ico.Games_LastWarningGameId] FOREIGN KEY ([LastWarningGameId]) REFERENCES [ico].[Games] ([Id])
    ALTER TABLE [ico].[AccountFeedbacks] ADD CONSTRAINT [FK_ico.AccountFeedbacks_ico.Games_GameId] FOREIGN KEY ([GameId]) REFERENCES [ico].[Games] ([Id])
    CREATE INDEX [IX_LastWarningGameId] ON [ico].[Accounts]([LastWarningGameId])
    CREATE INDEX [IX_GameId] ON [ico].[AccountFeedbacks]([GameId])
    CREATE INDEX [IX_GameId] ON [ico].[AccountGameRecords]([GameId])
    CREATE INDEX [IX_GameId] ON [ico].[GameFeedbacks]([GameId])
    
    --__MigrationHistory excluded
    

    我不确定这是否是最佳解决方案。但它似乎确实在我的开发机器上工作。遗憾的是,由于"此版本的SQL Server不支持没有聚簇索引的表,因此无法在Azure上运行。请创建聚簇索引,然后重试。"。这是由于ALTER TABLE [ico]。[游戏] DROP CONSTRAINT [PK_ico.Games]。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

使用Azure,我最终放弃了表格(将其从我的上下文中删除),然后重新搭建它。只有当你能够承受丢失游戏桌中的所有数据时,这才有效。对我来说,我决定在部署之前更改我的主键,因此我的表中只有测试数据。