Skip to content

EF Migration evaluating the script #24045

Closed as not planned
Closed as not planned

Description

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20201009100044_InitialCreate')
BEGIN
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'Id', N'OrganisationId') AND [object_id] = OBJECT_ID(N'[Config]'))
SET IDENTITY_INSERT [Config] ON;
INSERT INTO [Config] ([Id], [OrganisationId])
VALUES (1, 1);
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'Id', N'OrganisationId') AND [object_id] = OBJECT_ID(N'[Config]'))
SET IDENTITY_INSERT [Config] OFF;
END;

IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20201119082239_ChangedConfig')
BEGIN
DECLARE @var0 sysname;
SELECT @var0 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Config]') AND [c].[name] = N'OrganisationId');
IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [Config] DROP CONSTRAINT [' + @var0 + '];');
ALTER TABLE [Config] DROP COLUMN [OrganisationId];
END;

So, the initial create had a table Config with OrganisationId as a field. In a later build, it was dropped.
The migration worked fine at that point. Now, I have made more changes to the table, and the migration script fails with an error "Invalid column name".

Any idea why this is happening? The __EFMigrationsHistory does have the migrationIds, so looks like the script is getting evaluated before execution.

Any solution for this? Or am I missing something really simple?

Thanks for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions