Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EF Migration evaluating the script #24045

Closed
VidSankar opened this issue Feb 2, 2021 · 2 comments
Closed

EF Migration evaluating the script #24045

VidSankar opened this issue Feb 2, 2021 · 2 comments

Comments

@VidSankar
Copy link

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.

@ajcvickers
Copy link
Member

@VidSankar This looks like the kind of thing that was fixed for EF Core 5.0 as part of #12911. What version of EF Core are you using?

@VidSankar
Copy link
Author

@VidSankar This looks like the kind of thing that was fixed for EF Core 5.0 as part of #12911. What version of EF Core are you using?

Current version I am using is 3.1.8. Will upgrade and check if it resolves. Thank you :)

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

No branches or pull requests

3 participants