You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
migrationBuilder.InsertData generates SQL with 'Invalid column name' in idempotent scripts #17826
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20190827132005_AddMyEntity')
BEGIN
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'EntityId', N'MyRemovedColumn') AND [object_id] = OBJECT_ID(N'[MyEntity]'))
SET IDENTITY_INSERT [MyEntity] ON;
INSERT INTO [MyEntity] ([EntityId], [MyRemovedColumn])
VALUES (1, N'Stuff');
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'EntityId', N'MyRemovedColumn') AND [object_id] = OBJECT_ID(N'[MyEntity]'))
SET IDENTITY_INSERT [MyEntity] OFF;
END;
If this migration has already been run then this part of the script produced the reported error even though it doesn't get run. It appears a way to prevent this error is to wrap the INSERT in an EXEC like so:
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'20190827132005_AddMyEntity')
BEGIN
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'EntityId', N'MyRemovedColumn') AND [object_id] = OBJECT_ID(N'[MyEntity]'))
SET IDENTITY_INSERT [MyEntity] ON;
EXEC('
INSERT INTO [MyEntity] ([EntityId], [MyRemovedColumn])
VALUES (1, N''Stuff'');
');
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'EntityId', N'MyRemovedColumn') AND [object_id] = OBJECT_ID(N'[MyEntity]'))
SET IDENTITY_INSERT [MyEntity] OFF;
END;
Activity
bricelam commentedon Sep 13, 2019
Duplicate of #12911