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
dotnet ef migrations add Initial
dotnet ef database update
Update schema adding a new property
// Updated schemapublicrecordEmployee{publicintId{get;set;}publicstringName{get;set;}// Added a new propertypublicstringDepartment{get;set;}}
Add a migration for the updated schema
dotnet ef migrations add Secondary
Modify migration to execute custom SQL
publicpartialclassSecondary:Migration{/// <inheritdoc />protectedoverridevoidUp(MigrationBuildermigrationBuilder){migrationBuilder.AddColumn<string>(name:"Department",table:"Employees",type:"nvarchar(max)",nullable:false,defaultValue:"");// Custom SQLmigrationBuilder.Sql("UPDATE Employees SET Department = 'IT'");}/// <inheritdoc />protectedoverridevoidDown(MigrationBuildermigrationBuilder){migrationBuilder.DropColumn(name:"Department",table:"Employees");}}
Generate migration Script
dotnet ef migrations script --idempotent
Generated Script
IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULLBEGIN
CREATE TABLE [__EFMigrationsHistory] (
[MigrationId] nvarchar(150) NOT NULL,
[ProductVersion] nvarchar(32) NOT NULL,
CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
);
END;
GO
BEGIN TRANSACTION;
IF NOT EXISTS (
SELECT*FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20250107175345_Initial'
)
BEGIN
CREATE TABLE [Employees] (
[Id] intNOT NULL IDENTITY,
[Name] nvarchar(max) NOT NULL,
CONSTRAINT [PK_Employees] PRIMARY KEY ([Id])
);
END;
IF NOT EXISTS (
SELECT*FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20250107175345_Initial'
)
BEGININSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20250107175345_Initial', N'9.0.0');
END;
IF NOT EXISTS (
SELECT*FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20250107175433_Secondary'
)
BEGINALTERTABLE [Employees] ADD [Department] nvarchar(max) NOT NULL DEFAULT N'';
END;
IF NOT EXISTS (
SELECT*FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20250107175433_Secondary'
)
BEGINUPDATE Employees SET Department ='IT'
END;
IF NOT EXISTS (
SELECT*FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20250107175433_Secondary'
)
BEGININSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20250107175433_Secondary', N'9.0.0');
END;
COMMIT;
GO
Try to execute in SSMS (same error with invoke-sqlcmd)
Msg 207, Level 16, State 1, Line 46
Invalid column name 'Department'.
Include provider and version information
EF Core version: 9.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: 9.0.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.13.0 Preview 2.1
Consider the following simple Console Application that uses
EF Core 9.0
Generated Script
Try to execute in SSMS (same error with
invoke-sqlcmd
)Include provider and version information
EF Core version: 9.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: 9.0.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.13.0 Preview 2.1
A video of steps to reproduce:
https://github.com/user-attachments/assets/7ac16d34-b9f8-4b3a-a53e-9d5f73a3abc3
Sample project:
EfCore9.zip
The text was updated successfully, but these errors were encountered: