Skip to content

Fix alter column type JSON to nVarchar - #38464

Merged
AndriySvyryd merged 10 commits into
dotnet:mainfrom
atiq-bs23:fix-json-to-nvarchar-column-migration
Jun 24, 2026
Merged

AndriySvyryd merged 10 commits into
dotnet:mainfrom
atiq-bs23:fix-json-to-nvarchar-column-migration

Conversation

@atiq-bs23

Copy link
Copy Markdown
Contributor

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
Contributor Author

@dotnet-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 json→nvarchar(max) (nullable, non-nullable, idempotent) and that nvarchar→json still uses ALTER COLUMN.
  • Added a functional test covering runtime execution for json→nvarchar(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 nvarchar→json remains unchanged.
test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs Adds an end-to-end functional test executing the json→nvarchar(max) workaround.

@atiq-bs23

Copy link
Copy Markdown
Contributor 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

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd enabled auto-merge (squash) June 24, 2026 16:39
@AndriySvyryd
AndriySvyryd merged commit b56cdec into dotnet:main Jun 24, 2026
13 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jun 25, 2026
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