diff --git a/src/EFCore.Relational/Update/Internal/CommandBatchPreparer.cs b/src/EFCore.Relational/Update/Internal/CommandBatchPreparer.cs index 7c5457b00fd..4e7c34dbeee 100644 --- a/src/EFCore.Relational/Update/Internal/CommandBatchPreparer.cs +++ b/src/EFCore.Relational/Update/Internal/CommandBatchPreparer.cs @@ -57,14 +57,12 @@ public CommandBatchPreparer(CommandBatchPreparerDependencies dependencies) var parameterNameGenerator = Dependencies.ParameterNameGeneratorFactory.Create(); var commands = CreateModificationCommands(entries, updateAdapter, parameterNameGenerator.GenerateNext); var sortedCommandSets = TopologicalSort(commands); + var batch = Dependencies.ModificationCommandBatchFactory.Create(); - for (var commandSetIndex = 0; commandSetIndex < sortedCommandSets.Count; commandSetIndex++) + foreach (var independentCommandSet in sortedCommandSets) { - var independentCommandSet = sortedCommandSets[commandSetIndex]; - independentCommandSet.Sort(Dependencies.ModificationCommandComparer); - var batch = Dependencies.ModificationCommandBatchFactory.Create(); foreach (var modificationCommand in independentCommandSet) { (modificationCommand as ModificationCommand)?.AssertColumnsNotInitialized(); @@ -106,34 +104,32 @@ public CommandBatchPreparer(CommandBatchPreparerDependencies dependencies) batch = StartNewBatch(parameterNameGenerator, modificationCommand); } } + } - var hasMoreCommandSets = commandSetIndex < sortedCommandSets.Count - 1; - - if (batch.ModificationCommands.Count == 1 - || batch.ModificationCommands.Count >= _minBatchSize) + if (batch.ModificationCommands.Count == 1 + || batch.ModificationCommands.Count >= _minBatchSize) + { + if (batch.ModificationCommands.Count > 1) { - if (batch.ModificationCommands.Count > 1) - { - Dependencies.UpdateLogger.BatchReadyForExecution( - batch.ModificationCommands.SelectMany(c => c.Entries), batch.ModificationCommands.Count); - } + Dependencies.UpdateLogger.BatchReadyForExecution( + batch.ModificationCommands.SelectMany(c => c.Entries), batch.ModificationCommands.Count); + } - batch.Complete(); + batch.Complete(); - yield return (batch, hasMoreCommandSets); - } - else - { - Dependencies.UpdateLogger.BatchSmallerThanMinBatchSize( - batch.ModificationCommands.SelectMany(c => c.Entries), batch.ModificationCommands.Count, _minBatchSize); + yield return (batch, false); + } + else + { + Dependencies.UpdateLogger.BatchSmallerThanMinBatchSize( + batch.ModificationCommands.SelectMany(c => c.Entries), batch.ModificationCommands.Count, _minBatchSize); - for (var commandIndex = 0; commandIndex < batch.ModificationCommands.Count; commandIndex++) - { - var singleCommandBatch = StartNewBatch(parameterNameGenerator, batch.ModificationCommands[commandIndex]); - singleCommandBatch.Complete(); + for (var commandIndex = 0; commandIndex < batch.ModificationCommands.Count; commandIndex++) + { + var singleCommandBatch = StartNewBatch(parameterNameGenerator, batch.ModificationCommands[commandIndex]); + singleCommandBatch.Complete(); - yield return (singleCommandBatch, hasMoreCommandSets || commandIndex < batch.ModificationCommands.Count - 1); - } + yield return (singleCommandBatch, commandIndex < batch.ModificationCommands.Count - 1); } } }