diff --git a/src/EFCore.Design/Migrations/Design/CSharpMigrationOperationGenerator.cs b/src/EFCore.Design/Migrations/Design/CSharpMigrationOperationGenerator.cs index f7f702e97c0..f35a089e6d1 100644 --- a/src/EFCore.Design/Migrations/Design/CSharpMigrationOperationGenerator.cs +++ b/src/EFCore.Design/Migrations/Design/CSharpMigrationOperationGenerator.cs @@ -1805,11 +1805,14 @@ protected virtual void Generate(RestartSequenceOperation operation, IndentedStri .Append(Code.Literal(operation.Schema)); } - builder - .AppendLine(",") - .Append("startValue: ") - .Append(Code.Literal(operation.StartValue)) - .Append(")"); + if (operation.StartValue.HasValue) + { + builder + .AppendLine(",") + .Append("startValue: ") + .Append(Code.Literal(operation.StartValue)) + .Append(")"); + } Annotations(operation.GetAnnotations(), builder); } diff --git a/src/EFCore.Relational/Migrations/MigrationBuilder.cs b/src/EFCore.Relational/Migrations/MigrationBuilder.cs index a22d60f3403..e418a750c43 100644 --- a/src/EFCore.Relational/Migrations/MigrationBuilder.cs +++ b/src/EFCore.Relational/Migrations/MigrationBuilder.cs @@ -1238,7 +1238,7 @@ public virtual OperationBuilder RenameTable( /// A builder to allow annotations to be added to the operation. public virtual OperationBuilder RestartSequence( string name, - long startValue = 1L, + long? startValue = null, string? schema = null) { Check.NotEmpty(name, nameof(name)); diff --git a/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs b/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs index d3bd0f1f72f..ea675ea60d7 100644 --- a/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs +++ b/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs @@ -793,9 +793,14 @@ protected virtual void Generate( builder .Append("ALTER SEQUENCE ") - .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name, operation.Schema)) - .Append(" RESTART WITH ") - .Append(longTypeMapping.GenerateSqlLiteral(operation.StartValue)) + .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name, operation.Schema)); + if (operation.StartValue.HasValue) + { + builder + .Append(" RESTART WITH ") + .Append(longTypeMapping.GenerateSqlLiteral(operation.StartValue)); + } + builder .AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); EndStatement(builder); diff --git a/src/EFCore.Relational/Migrations/Operations/RestartSequenceOperation.cs b/src/EFCore.Relational/Migrations/Operations/RestartSequenceOperation.cs index af2fb650b3c..8ff05f69289 100644 --- a/src/EFCore.Relational/Migrations/Operations/RestartSequenceOperation.cs +++ b/src/EFCore.Relational/Migrations/Operations/RestartSequenceOperation.cs @@ -23,7 +23,7 @@ public class RestartSequenceOperation : MigrationOperation public virtual string? Schema { get; set; } /// - /// The value at which the sequence should re-start, defaulting to 1. + /// The value at which the sequence should re-start, of if the start value should not be specified . /// - public virtual long StartValue { get; set; } = 1L; + public virtual long? StartValue { get; set; } = 1L; } diff --git a/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs b/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs index 5ad499c5698..49460a11383 100644 --- a/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs +++ b/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs @@ -540,9 +540,14 @@ protected override void Generate( { builder .Append("ALTER SEQUENCE ") - .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name, operation.Schema)) - .Append(" RESTART WITH ") - .Append(IntegerConstant(operation.StartValue)) + .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name, operation.Schema)); + if (operation.StartValue.HasValue) + { + builder + .Append(" RESTART WITH ") + .Append(IntegerConstant(operation.StartValue.Value)); + } + builder .AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); EndStatement(builder);