Skip to content

Fix alter column type JSON to nVarchar#38464

Open
atiq-bs23 wants to merge 8 commits into
dotnet:mainfrom
atiq-bs23:fix-json-to-nvarchar-column-migration
Open

Fix alter column type JSON to nVarchar#38464
atiq-bs23 wants to merge 8 commits into
dotnet:mainfrom
atiq-bs23:fix-json-to-nvarchar-column-migration

Conversation

@atiq-bs23

Copy link
Copy Markdown

Fixes #38364

SQL Server's native json type cannot be implicitly converted to nvarchar in ALTER COLUMN. When the old column type is json, the generator now uses rename → add → CONVERT-copy → drop instead.

  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

@atiq-bs23 atiq-bs23 requested a review from a team as a code owner June 19, 2026 07:49
@atiq-bs23

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes SQL Server migrations for ALTER COLUMN when converting a column from the native json type to nvarchar(...), which SQL Server doesn’t allow via implicit conversion. The SQL generator now emits a rename → add → CONVERT copy → drop flow instead of a direct ALTER COLUMN.

Changes:

  • Updated SqlServerMigrationsSqlGenerator to special-case json→non-json type changes and generate a safe copy-based migration sequence.
  • Added SQL generation tests validating the emitted DDL/DML for jsonnvarchar(max) (nullable, non-nullable, idempotent) and that nvarcharjson still uses ALTER COLUMN.
  • Added a functional test covering runtime execution for jsonnvarchar(max) conversion (nullable case).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs Implements rename/add/convert-copy/drop workaround for json→non-json ALTER COLUMN on SQL Server.
test/EFCore.SqlServer.FunctionalTests/Migrations/SqlServerMigrationsSqlGeneratorTest.cs Adds SQL assertion tests for the new generation path and ensures nvarcharjson remains unchanged.
test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs Adds an end-to-end functional test executing the jsonnvarchar(max) workaround.

Comment thread src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs Outdated
Comment thread src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs Outdated
Comment thread src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs Outdated
Comment thread src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs Outdated
@atiq-bs23

Copy link
Copy Markdown
Author

Hi @AndriySvyryd
Thanks for the thorough feedback. I have resolved all the comments:

  • Added annotation copying (excluding identity) and Comment to the replacement column in AlterColumnFromJson, matching the drop+add path.

  • Updated the integration test to declare an index on the converted column — used nvarchar(450) instead of nvarchar(max) since SQL Server doesn't support indexing nvarchar(max) as a key column.

Could you please take another look when you get a chance?

One note on running the tests locally: Version.Details.props references 11.0.0-preview.6.26313.102 which isn't publicly available yet, so the test runner fails to resolve the runtime. I was running tests against preview.5 locally as a workaround. This should not affect the PR itself but wanted to flag it in case it causes issues in CI.

@atiq-bs23 atiq-bs23 requested a review from AndriySvyryd June 20, 2026 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EF Core 10 generates invalid down migration for MSSQL nvarchar(max) to json upgrade

3 participants