From 16b0cb864d147a82c050e662aa2be8249f788962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0lker=20amca?= Date: Fri, 31 Jul 2026 00:32:57 +0300 Subject: [PATCH] Emit oldStored based on the old column in CSharpMigrationOperationGenerator - The oldStored: argument of a generated AlterColumn call was gated on operation.IsStored (the new column) while emitting operation.OldColumn.IsStored, so altering a column away from a stored computed column dropped oldStored: from the generated (and reverse) migration - Gate on operation.OldColumn.IsStored to match the value being written - Add a test covering alter from a stored computed column to a non-computed column --- .../CSharpMigrationOperationGenerator.cs | 2 +- .../CSharpMigrationOperationGeneratorTest.cs | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/EFCore.Design/Migrations/Design/CSharpMigrationOperationGenerator.cs b/src/EFCore.Design/Migrations/Design/CSharpMigrationOperationGenerator.cs index 9c57eebf295..89af76dcb32 100644 --- a/src/EFCore.Design/Migrations/Design/CSharpMigrationOperationGenerator.cs +++ b/src/EFCore.Design/Migrations/Design/CSharpMigrationOperationGenerator.cs @@ -640,7 +640,7 @@ protected virtual void Generate(AlterColumnOperation operation, IndentedStringBu .Append("oldComputedColumnSql: ") .Append(Code.Literal(operation.OldColumn.ComputedColumnSql)); - if (operation.IsStored != null) + if (operation.OldColumn.IsStored != null) { builder .AppendLine(",") diff --git a/test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationOperationGeneratorTest.cs b/test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationOperationGeneratorTest.cs index 3eb017d6fcf..20728837737 100644 --- a/test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationOperationGeneratorTest.cs +++ b/test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationOperationGeneratorTest.cs @@ -633,6 +633,37 @@ public void AlterColumnOperation_all_args() Assert.Equal("Some Collation", o.OldColumn.Collation); }); + [Fact] + public void AlterColumnOperation_computed_to_non_computed_preserves_oldStored() + => Test( + new AlterColumnOperation + { + Name = "Id", + Table = "Post", + ClrType = typeof(int), + OldColumn = + { + ComputedColumnSql = "1", + IsStored = true + } + }, + """ +mb.AlterColumn( + name: "Id", + table: "Post", + nullable: false, + oldComputedColumnSql: "1", + oldStored: true); +""", + o => + { + Assert.Equal("Id", o.Name); + Assert.Equal("Post", o.Table); + Assert.Equal(typeof(int), o.ClrType); + Assert.Equal("1", o.OldColumn.ComputedColumnSql); + Assert.True(o.OldColumn.IsStored); + }); + [Fact] public void AlterColumnOperation_DefaultValueSql() => Test(