Problems changing PK datatype using EntityFramework CodeFirst migrations on Azure

白昼怎懂夜的黑 提交于 2019-12-14 02:32:40

问题


I have a code-first EntityFramework project hosted on Azure. Currently I handle migrations by

  1. Run Add-Migration from Package Manager Console
  2. Run Update-Database -script from Package Manager Console getting me an .sql script
  3. Manually run the script on Azure after testing on my dev environment

This has been working pretty smoothly so far. But I am running into some serious issues trying to change a primary key of one of my tables from an int to a long.

Here is the migration script that got generated from 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

Running this script won't work at all due to INDEX and FK CONSTRAINTS. So firstly I manually changed the script as so:

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

I'm not sure if this is the best solution. But it does seem to work on my dev machine. Unfortunately it will not work on Azure due to "Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again.". This is due to ALTER TABLE [ico].[Games] DROP CONSTRAINT [PK_ico.Games]. Any suggestions?


回答1:


With Azure, I just ended up dropping the table (removing it from my Context) and then re-scaffolding it. This only works if you can afford to lose all the data in your Games table, of course. For me, I decided to change my Primary Key before deployment and thus I only had test data in my table.



来源:https://stackoverflow.com/questions/23432187/problems-changing-pk-datatype-using-entityframework-codefirst-migrations-on-azur

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!