我有一个项目,我已经开发了两年,并且有很多迁移。但是,我最近遇到了一个创建自动迁移的问题,它会撤消我的显式迁移,这意味着我无法再正确构建数据库。它发生在多个(但并非全部)迁移上,下面是一个示例(来自使用-script),其中包含一个简化版本的简洁版本,以便于阅读:
简明版
--first migration (201608150953102_Table1Migration)
ALTER TABLE [dbo].[Table1] ADD [CancelledReason] [int]
--explicit migration recorded in __MigrationHistory
--automatic migration added (201608151201077_Table2Migration_AutomaticMigration)
ALTER TABLE [dbo].[Table1] DROP COLUMN [CancelledReason]
--automatic migration recorded in __MigrationHistory
--second migration (201608151201078_Table2Migration)
ALTER TABLE [dbo].[Table2] DROP COLUMN [ManagerId]
--explicit migration recorded in __MigrationHistory
--automatic migration added (201609091123044_Table3Migration_AutomaticMigration)
ALTER TABLE [dbo].[Table1] ADD [CancelledReason] [int]
--automatic migration recorded in __MigrationHistory
--third migration (201609091123045_Table3Migration)
ALTER TABLE [dbo].[Table3] ADD [Active] [bit] NOT NULL DEFAULT 1
--explicit migration recorded in __MigrationHistory
详细版本:
ALTER TABLE [dbo].[Table1] ADD [CancelledReason] [int]
INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'201608150953102_Table1Migration', N'Project.Migrations.Configuration', 0x1F8[...] , N'6.1.3-40302')
DECLARE @var0 nvarchar(128)
SELECT @var0 = name
FROM sys.default_constraints
WHERE parent_object_id = object_id(N'dbo.Table1')
AND col_name(parent_object_id, parent_column_id) = 'CancelledReason';
IF @var0 IS NOT NULL
EXECUTE('ALTER TABLE [dbo].[Table1] DROP CONSTRAINT [' + @var0 + ']')
ALTER TABLE [dbo].[Table1] DROP COLUMN [CancelledReason]
INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'201608151201077_Table2Migration_AutomaticMigration', N'Project.Migrations.Configuration', 0x1F8[...] , N'6.1.3-40302')
IF object_id(N'[dbo].[FK_dbo.Table2_dbo.AspNetUsers_ManagerId]', N'F') IS NOT NULL
ALTER TABLE [dbo].[Table2] DROP CONSTRAINT [FK_dbo.Table2_dbo.AspNetUsers_ManagerId]
IF EXISTS (SELECT name FROM sys.indexes WHERE name = N'IX_ManagerId' AND object_id = object_id(N'[dbo].[Table2]', N'U'))
DROP INDEX [IX_ManagerId] ON [dbo].[Table2]
ALTER TABLE [dbo].[Table2] ADD [Manager] [nvarchar](max)
DECLARE @var1 nvarchar(128)
SELECT @var1 = name
FROM sys.default_constraints
WHERE parent_object_id = object_id(N'dbo.Table2')
AND col_name(parent_object_id, parent_column_id) = 'ManagerId';
IF @var1 IS NOT NULL
EXECUTE('ALTER TABLE [dbo].[Table2] DROP CONSTRAINT [' + @var1 + ']')
ALTER TABLE [dbo].[Table2] DROP COLUMN [ManagerId]
INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'201608151201078_Table2Migration', N'Project.Migrations.Configuration', 0x1F8[...] , N'6.1.3-40302')
ALTER TABLE [dbo].[Table1] ADD [CancelledReason] [int]
INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'201609091123044_Table3Migration_AutomaticMigration', N'Project.Migrations.Configuration', 0x1F8[...] , N'6.1.3-40302')
ALTER TABLE [dbo].[Table3] ADD [Active] [bit] NOT NULL DEFAULT 1
INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'201609091123045_Table3Migration', N'Project.Migrations.Configuration', 0x1F8[...] , N'6.1.3-40302')
您可以看到我专注于三个并发迁移(这些迁移大约有20到30次),因为它们以简单的方式显示问题。在下一次迁移首先触发自动迁移之前,第一次迁移会按预期添加一列,从而消除第一次迁移的更改。然后第三次迁移会导致另一次自动迁移,这会将列添加回来。
我不明白为什么会这样,所以我希望有人可以对此有所了解。
其他可能的相关信息: