Skip to content

script-migration of an example in the doc doesn't work (adding raw SQL) #35662

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

Closed
yepeekai opened this issue Feb 20, 2025 · 3 comments
Closed

Comments

@yepeekai
Copy link

Bug description

https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/managing?tabs=dotnet-core-cli#adding-raw-sql

When doing exactly that, the script doesn't work because there is no GO between statements anymore.

the script complains that column FullName doesn't exist

Your code

//copied from the docs

migrationBuilder.AddColumn<string>(
    name: "FullName",
    table: "Customer",
    nullable: true);

migrationBuilder.Sql(
@"
    UPDATE Customer
    SET FullName = FirstName + ' ' + LastName;
");

migrationBuilder.DropColumn(
    name: "FirstName",
    table: "Customer");

migrationBuilder.DropColumn(
    name: "LastName",
    table: "Customer");

Stack traces


Verbose output


EF Core version

9.0.0

Database provider

No response

Target framework

No response

Operating system

No response

IDE

No response

@maumar
Copy link
Contributor

maumar commented Feb 20, 2025

what is the migration script that gets generated?

When I try to script the migration you provided, i get the following script:

BEGIN TRANSACTION;
ALTER TABLE [Customer] ADD [FullName] nvarchar(max) NULL;


    UPDATE Customer
    SET FullName = FirstName + ' ' + LastName;


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'[Customer]') AND [c].[name] = N'FirstName');
IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [Customer] DROP CONSTRAINT [' + @var0 + '];');
ALTER TABLE [Customer] DROP COLUMN [FirstName];

DECLARE @var1 sysname;
SELECT @var1 = [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'[Customer]') AND [c].[name] = N'LastName');
IF @var1 IS NOT NULL EXEC(N'ALTER TABLE [Customer] DROP CONSTRAINT [' + @var1 + '];');
ALTER TABLE [Customer] DROP COLUMN [LastName];

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20250220212315_Mig2', N'9.0.0');

COMMIT;
GO

which works just fine

@yepeekai
Copy link
Author

On my computer with sql server 2022 express (16.0.1135.2), I get the following error using the script copied from your post:
Msg 207, Level 16, State 1, Line 5
Invalid column name 'FullName'.

@yepeekai
Copy link
Author

EF 8 worked just fine because there were GO statements everywhere

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