{"record":{"id":"9752a68cfc65a734","repo":"elsa-workflows/elsa-core","slug":"unable-to-update-workflowdefinition-with-ids-string-join","errorCode":null,"errorMessage":"Unable to update WorkflowDefinition with ids {string.Join(',', duplicates)} multiple times.","messagePattern":"Unable to update WorkflowDefinition with ids (.+?) multiple times\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/modules/Elsa.Workflows.Runtime/Services/DefaultWorkflowDefinitionStorePopulator.cs","lineNumber":251,"sourceCode":"        workflowDefinition.MaterializerContext = materializerContextJson;\n        workflowDefinition.MaterializerName = materializedWorkflow.MaterializerName;\n\n        if (existingDefinitionVersion is null && workflowDefinitionsToSave.Any(w => w.Id == workflowDefinition.Id))\n        {\n            _logger.LogInformation(\"Workflow with ID {WorkflowId} already exists\", workflowDefinition.Id);\n            return workflowDefinition;\n        }\n\n        workflowDefinitionsToSave.Add(workflowDefinition);\n\n        var duplicates = workflowDefinitionsToSave.GroupBy(wd => wd.Id)\n            .Where(g => g.Count() > 1)\n            .Select(g => g.Key)\n            .ToList();\n\n        if (duplicates.Any())\n        {\n            throw new Exception($\"Unable to update WorkflowDefinition with ids {string.Join(',', duplicates)} multiple times.\");\n        }\n\n        await _workflowDefinitionStore.SaveManyAsync(workflowDefinitionsToSave, cancellationToken);\n        return workflowDefinition;\n\n        async Task UpdateIsLatest()\n        {\n            // Always try to update the IsLatest property based on the VersionNumber\n\n            // Reset current latest definitions.\n            var filter = new WorkflowDefinitionFilter\n            {\n                DefinitionId = definitionId,\n                VersionOptions = VersionOptions.Latest\n            };\n            var latestWorkflowDefinitions = (await _workflowDefinitionStore.FindManyAsync(filter, cancellationToken)).ToList();\n\n            // If the latest definitions contains definitions with the same ID then we need to replace them with the latest workflow definitions.","sourceCodeStart":233,"sourceCodeEnd":269,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.Workflows.Runtime/Services/DefaultWorkflowDefinitionStorePopulator.cs#L233-L269","documentation":"DefaultWorkflowDefinitionStorePopulator's AddOrUpdateCoreAsync detects duplicate workflow definition IDs within a single batch of definitions to upsert. Duplicates would be saved multiple times in one SaveManyAsync call, corrupting the 'latest' flag logic, so it throws with the list of duplicated IDs.","triggerScenarios":"A bulk registration/population pass (e.g. scanning multiple assemblies or re-reading the same workflow file twice) that produces two or more WorkflowDefinitions sharing the same ID in one batch.","commonSituations":"Duplicate workflow files with the same definition ID in different locations; assembly scans loading the same assembly twice; copied workflow JSON where the definition ID was not regenerated.","solutions":["Find the source of duplicate definition IDs (the IDs are listed in the message) and deduplicate the input batch.","Regenerate a unique definition ID for one of the duplicated workflow definitions (e.g. copy the JSON with a new ID).","Fix assembly/file scanning so each workflow source is loaded only once per populator run."],"exampleFix":"// before\n// two files contain the same definitionId: \"my-workflow\"\n// after\n// edit the copied file and set a fresh definitionId, or remove the duplicate file","handlingStrategy":"validation","validationCode":"var dupes = definitions.GroupBy(d => d.Id).Where(g => g.Count() > 1).Select(g => g.Key).ToList();\nif (dupes.Any()) throw new InvalidOperationException($\"Duplicate definition ids in batch: {string.Join(',', dupes)}\");","typeGuard":null,"tryCatchPattern":"try { await populator.PopulateAsync(definitions); } catch (Exception ex) when (ex.Message.StartsWith(\"Unable to update WorkflowDefinition\")) { logger.LogError(ex, \"Duplicate definition ids during registration\"); }","preventionTips":["Deduplicate definitions by ID before bulk population","Ensure each workflow file/assembly is scanned only once","Regenerate definition IDs when copying workflow JSON","Group scanned sources by definition ID during discovery"],"tags":["workflow-definitions","bulk-registration","duplicate-ids"],"backgroundTag":"internal-invariant-violation","analyzedSha":"fe9217bdfa0e27f0e09e45006eb6898f616e513d","analyzedAt":"2026-09-13T20:32:34.702Z","contentChangedAt":"2026-09-13T20:32:34.702Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}