{"record":{"id":"f77e998ee735e8b0","repo":"dotnet/efcore","slug":"command-entitystate-tostring","errorCode":null,"errorMessage":"command.EntityState.ToString()","messagePattern":"command\\.EntityState\\.ToString\\(\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs","lineNumber":2437,"sourceCode":"                            .Select(c => (IColumn)c.Column!);\n                        var anyKeyColumnDropped = keyColumns.Any(c => diffContext.FindDrop(c) != null);\n\n                        yield return new DeleteDataOperation\n                        {\n                            Schema = command.Schema,\n                            Table = command.TableName,\n                            KeyColumns = command.ColumnModifications.Where(col => col.IsKey).Select(col => col.ColumnName).ToArray(),\n                            KeyColumnTypes = anyKeyColumnDropped\n                                ? keyColumns.Select(col => col.StoreType).ToArray()\n                                : null,\n                            KeyValues = ToMultidimensionalArray(\n                                command.ColumnModifications.Where(col => col.IsKey).Select(col => col.Value).ToArray()),\n                            IsDestructiveChange = true\n                        };\n\n                        break;\n                    default:\n                        throw new InvalidOperationException(command.EntityState.ToString());\n                }\n            }\n\n            if (batchInsertOperation != null)\n            {\n                yield return batchInsertOperation;\n            }\n        }\n    }\n\n    #endregion\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>","sourceCodeStart":2419,"sourceCodeEnd":2455,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs#L2419-L2455","documentation":"Thrown at the `default` arm of a switch over `command.EntityState` in MigrationsModelDiffer seed-operation generation. Only Added/Modified/Deleted are handled; any other EntityState value is an internal invariant violation. The thrown message is just the raw enum string, indicating an unexpected/broken command state rather than a user configuration problem.","triggerScenarios":"Internal/profiler/test code constructs a ModificationCommand with an EntityState other than Added, Modified, or Deleted (e.g. Unchanged, Detached) and feeds it into the differ's seed-operation generator. Not reachable through normal `HasData`/`Migrate` flows.","commonSituations":"Custom modification-command pipelines; third-party tooling or a buggy EF provider feeding Detached/Unchanged commands into the differ; an EF Core internal regression after a version upgrade.","solutions":["If you are feeding custom commands, ensure each command's `EntityState` is Added, Modified, or Deleted before diffing.","If reached through normal EF usage, capture a repro and report an EF Core bug (this is an internal contract violation).","Upgrade or downgrade EF Core to a version without the regression, then re-test."],"exampleFix":"// before\ncommand.EntityState = EntityState.Unchanged; // fed to differ -> throws\n// after\ncommand.EntityState = EntityState.Modified;","handlingStrategy":"try-catch","validationCode":"// If you construct commands manually, restrict EntityState to seed-relevant values.\nif (command.EntityState is not (EntityState.Added or EntityState.Modified or EntityState.Deleted))\n    throw new ArgumentOutOfRangeException(nameof(command.EntityState), command.EntityState, \"Must be Added, Modified, or Deleted.\");","typeGuard":"bool IsSeedEntityState(EntityState s) => s is EntityState.Added or EntityState.Modified or EntityState.Deleted;","tryCatchPattern":"try { /* differ / migrate */ }\ncatch (InvalidOperationException ex) when (ex.Message is \"Added\" or \"Modified\" or \"Deleted\" or \"Unchanged\" or \"Detached\")\n{\n    // internal invariant violation: log command source + EntityState, file an EF Core bug.\n    logger.LogCritical(ex, \"Unexpected EntityState in differ pipeline.\");\n    throw;\n}","preventionTips":["Only feed Added/Modified/Deleted commands to the differ's seed-operation generator.","Treat this error as an EF Core internal bug if reached via normal HasData/Migrate; report it."],"tags":["efcore","migrations","seed-data","model-differ","internal-invariant","relational"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}