我必须托管在Azure中的代码优先的EntityFramework项目。 目前我办理移民通过
- 从运行程序包管理器控制台添加迁移
- 从软件包管理器控制台让我的.SQL脚本运行更新,数据库-script
- 在我的开发环境中进行测试后手动运行在Azure上的脚本
这已工作相当顺利至今。 但是我遇到了试图从一个int一个长期改变我的一个表的主键的一些严重的问题。
下面是接到的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约束工作。 所以,首先我手动改剧本像这样:
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
我不知道这是否是最好的解决办法。 但它似乎我的dev的机器上工作。 不幸的是它不会在Azure上由于工作“没有一个聚集索引的表并不在此版本的SQL Server的支持。请创建聚簇索引,然后再试一次。” 这是因为ALTER TABLE [ICO] [游戏] DROP CONSTRAINT [PK_ico.Games。 有什么建议么?