diff --git a/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs b/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs index 07dc3e9d6b8..41a6029ba1a 100644 --- a/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs +++ b/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs @@ -20,7 +20,6 @@ public class MigrationsModelDiffer : IMigrationsModelDiffer { typeof(DropIndexOperation), typeof(DropPrimaryKeyOperation), - typeof(DropSequenceOperation), typeof(DropUniqueConstraintOperation), typeof(DropCheckConstraintOperation) }; @@ -131,6 +130,7 @@ protected virtual IReadOnlyList Sort( var dropColumnOperations = new List(); var dropComputedColumnOperations = new List(); var dropTableOperations = new List(); + var dropSequenceOperations = new List(); var ensureSchemaOperations = new List(); var createSequenceOperations = new List(); var createTableOperations = new List(); @@ -173,6 +173,10 @@ protected virtual IReadOnlyList Sort( { dropTableOperations.Add((DropTableOperation)operation); } + else if (type == typeof(DropSequenceOperation)) + { + dropSequenceOperations.Add((DropSequenceOperation)operation); + } else if (type == typeof(EnsureSchemaOperation)) { ensureSchemaOperations.Add(operation); @@ -312,6 +316,7 @@ protected virtual IReadOnlyList Sort( .Concat(sourceDataOperations) .Concat(dropComputedColumnOperations) .Concat(dropColumnOperations) + .Concat(dropSequenceOperations) .Concat(ensureSchemaOperations) .Concat(renameTableOperations) .Concat(renameOperations) diff --git a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs index 5038a79aa12..c5f29cab1e3 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs @@ -2286,6 +2286,54 @@ await Test( EXEC(N'ALTER SCHEMA [' + @defaultSchema + N'] TRANSFER [TestSequenceSchema].[TestSequence];');"); } + [ConditionalFact] + public async Task Create_sequence_and_dependent_column() + { + await Test( + builder => builder.Entity("People").Property("Id"), + builder => { }, + builder => + { + builder.HasSequence("TestSequence"); + builder.Entity("People").Property("SeqProp").HasDefaultValueSql("NEXT VALUE FOR TestSequence"); + }, + model => + { + var sequence = Assert.Single(model.Sequences); + Assert.Equal("TestSequence", sequence.Name); + }); + + AssertSql( + @"CREATE SEQUENCE [TestSequence] AS int START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE NO CYCLE;", + // + @"ALTER TABLE [People] ADD [SeqProp] int NOT NULL DEFAULT (NEXT VALUE FOR TestSequence);"); + } + + [ConditionalFact] + public async Task Drop_sequence_and_dependent_column() + { + await Test( + builder => builder.Entity("People").Property("Id"), + builder => + { + builder.HasSequence("TestSequence"); + builder.Entity("People").Property("SeqProp").HasDefaultValueSql("NEXT VALUE FOR TestSequence"); + }, + builder => { }, + model => Assert.Empty(model.Sequences)); + + AssertSql( + @"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'[People]') AND [c].[name] = N'SeqProp'); +IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [People] DROP CONSTRAINT [' + @var0 + '];'); +ALTER TABLE [People] DROP COLUMN [SeqProp];", + // + @"DROP SEQUENCE [TestSequence];"); + } + public override async Task InsertDataOperation() { await base.InsertDataOperation();